Learn From - Interactive SQL



SQL HAVING Clause


HAVING clause is used along with the GROUP BY keyword to filter information to get the desired resultset.

SQL HAVING Clause Syntax


SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name HAVING aggregate_function(column_name) logical operator value

Below is shown the "CommissionTBL" table:

BAPerson Date Amount
Diego Roel 3/27/2009 12:00:00 AM 225.74
Diego Roel 4/7/2011 12:00:00 AM 325.25
Maria Anders 5/9/2011 12:00:00 AM 321.27
Thomas Hardy 11/22/2009 12:00:00 AM 421.73
Thomas Hardy 3/1/2011 12:00:00 AM 221.29
Yang Wang 9/3/2009 12:00:00 AM 445.73

SQL HAVING Clause Example


To get total Amount of Comission for all Persons who have got Commission more than once.
Here we have grouped by "BAPerson" Column.

Text Editor below has the SQL Query, click the "View Result" button, to see the result.

Select BAPerson, sum(Amount) as TotalAmount
from CommissionTBL
GROUP BY BAPerson
HAVING Count(BAPerson) > 1

To Retrieve Persons whose Total Commission Amount Exceeds 325.0

Select BAPerson, sum(Amount) as TotalAmount
from CommissionTBL
GROUP BY BAPerson
HAVING sum(Amount) > 325.0

SQL in Text Editor: