HAVING Clause
The HAVING
clause lets you filter groups after applying aggregation.
It works like WHERE
, but is used specifically with results that come from GROUP BY
.
Why Not Use WHERE?
The difference between WHERE
and HAVING
is that:
WHERE
filters rows before groupingHAVING
filters groups after aggregation
For example, if you want to find regions with an average sales above a certain amount, you must first group the data and then use HAVING to filter the grouped results.
Basic Syntax
The HAVING
clause is used after the GROUP BY
clause in a SELECT
statement.
HAVING Syntax
SELECT column, AGG_FUNCTION(column) FROM table_name GROUP BY column HAVING condition;
Example: Regions with High Sales
Assume you have a table called clients
and you want to find regions with total sales above 25000.
You can use the HAVING
clause to filter the results after the data is grouped.
Regions with high sales
SELECT region, SUM(sales) AS total_sales FROM clients GROUP BY region HAVING SUM(sales) > 25000;
Quiz
0 / 1
The HAVING clause in SQL is used to filter rows before they are grouped.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Generate
Tables
Execution Result