Home / SQL / Reference / Aggregating

SQL HAVING

Filter groups after aggregation.

Syntax

GROUP BY col
HAVING condition_on_aggregates

WHERE filters rows before grouping; HAVING filters the groups afterwards.

Example

Query (on the shop database used in the SQL course)

SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country
HAVING COUNT(*) >= 3;

Result

countrycustomers
Australia12
Nepal4
United Kingdom3

3 rows

Related

Learn it step by step in the free lesson Summarising Data, with exercises that check your queries.