HAVING Clause
The HAVING clause lets you filter groups after aggregation has been applied.
It works like WHERE, but specifically filters the results of grouped data.
Basic Syntax
The HAVING clause comes after GROUP BY in a SELECT statement.
HAVING Syntax
SELECT column, AGG_FUNCTION(column)
FROM table_name
GROUP BY column
HAVING condition;
Example: Regions with High Sales
Suppose you have a clients table and want to find regions where total sales exceed 25,000.
Use the HAVING clause to filter results after grouping.
Regions with high sales
SELECT region, SUM(sales) AS total_sales
FROM clients
GROUP BY region
HAVING SUM(sales) > 25000;
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.