Learn SQL Lesson
HAVING Filters After GROUP BY
`HAVING` runs after `GROUP BY`. Instead of filtering individual rows, it filters the aggregated groups.
That means this pattern works:
SELECT region, SUM(amount) AS total_amount
FROM sales
GROUP BY region
HAVING SUM(amount) > 2000
SQL first forms one group per region, calculates each total, and only then removes the groups that do not pass the `HAVING` condition.
Practice challenge
Return the regions whose total sales are greater than 2000. Show region and total_amount, ordered by region.