SQL HAVING

The HAVING clause is used in combination with the GROUP BY clause and the SQL aggregate functions. HAVING clause allows us to call the data conditionally on the column that return from using the SQL aggregate functions.

The SQL HAVING syntax is simple and looks like this:

SELECT [COLUMN NAME 1] , AGGREGATE FUNCTION ( [COLUMN NAME 2] )
FROM [TABLE NAME]
GROUP BY[COLUMN NAME 1]
HAVING AGGREGATE FUNCTION ( [COLUMN NAME 2] ) = [CONDITION]


EXAMPLE :

Let’s say, we want to get the departments from GameScores that total scores is more than 4000.

Table GameScores

PlayerName Department Scores
Jason IT 3000
Irene IT 1500
Jane Marketing 1000
David Marketing 2500
Paul HR 2000
James HR 2000

SQL statement :

SELECT Department, SUM(Scores)
FROM GameScores
GROUP BY Department
HAVING SUM(Scores)>4000

Result:

Department SUM(Scores)
IT 4500
posted on 2013-09-18 14:10  莫水千流  阅读(337)  评论(0编辑  收藏  举报