Home     SQL Basic     Contact Us     Site Map    

SQL BASIC

SQL SELECT

SQL WHERE

SQL DISTINCT

SQL AND

SQL OR

SQL NOT

SQL ORDER BY

SQL IN

SQL BETWEEN

SQL LIKE

SQL ALIAS

SQL AGGREGATE

>SQL COUNT

>SQL SUM

>SQL MAX

>SQL MIN

>SQL AVG

SQL GROUP BY

SQL HAVING

SQL INSERT

SQL UPDATE

SQL DELETE

SQL SELECT INTO

SQL CREATE DATABASE

SQL CREATE TABLE

SQL DROP TABLE

SQL DROP DATABASE

SQL CREATE INDEX

SQL PRIMARY KEY

SQL FOREIGN KEY

SQL ALTER TABLE

SQL TRUNCATE TABLE

SQL JOIN

SQL INNER JOIN

SQL OUTER JOIN

SQL CROSS JOIN

SQL UNION

SQL UNION ALL

SQL INTERSECT



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
PlayerNameDepartmentScores
JasonIT3000
IreneIT1500
JaneMarketing1000
DavidMarketing2500
PaulHR2000
JamesHR2000

SQL statement :

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

Result:

DepartmentSUM(Scores)
IT4500