Learn SQL
-- aggregations.having

HAVING — Filtering Groups in SQL

A WHERE for aggregated results

--concept

HAVING filters groups after aggregation, the way WHERE filters rows before it. “Show departments whose average salary exceeds 60,000” is impossible in WHERE (averages don’t exist yet there) — trivial with HAVING AVG(salary) > 60000. Sequence: WHERE → group → aggregate → HAVING → select.

--how it works
1

WHERE cannot see aggregates

WHERE runs before grouping — AVG(salary) does not exist yet there.

2

HAVING filters groups

GROUP BY department HAVING AVG(salary) > 60000 — the condition is evaluated per finished bucket.

3

Any aggregate condition works

HAVING COUNT(*) >= 2 finds groups with at least two members; SUM/MIN/MAX are equally legal.

4

WHERE and HAVING coexist

WHERE narrows rows first; HAVING narrows groups after — both in one query is normal.

5

Execution order is fixed

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY — memorise it once, debug forever.

--visualization
Engineeringavg = 83,000headcount = 3Salesavg = 55,000headcount = 2Marketingavg = 47,000headcount = 1WHERE filters rows BEFORE grouping · HAVING filters groups AFTER
Stage 1 / 5
GROUP BY department HAVING AVG(salary) > 60,000 — buckets form first, THEN get filtered.
--example
query.sql
1SELECT department, COUNT(*) AS headcount,
2 ROUND(AVG(salary), 0) AS avg_salary
3FROM employees
4GROUP BY department
5HAVING AVG(salary) > 60000;
Result
departmentheadcountavg_salary
Engineering383000

Sales (55k) and Marketing (47k) fail the threshold after grouping; Engineering alone survives. Slide the threshold in the visualization and watch buckets drop.

--variants

Average-salary threshold

Keep groups whose average clears 60000.

query.sql
1SELECT department,
2 COUNT(*) AS headcount,
3 ROUND(AVG(salary), 0) AS avg_salary
4FROM employees
5GROUP BY department
6HAVING AVG(salary) > 60000;
Result
departmentheadcountavg_salary
Engineering383000
1Groups form first: 83000, 55000, 47000.
2HAVING then removes Sales and Marketing — below threshold.
3WHERE AVG(salary) > 60000 would be an error: no averages exist at WHERE time.

Group-size filter

Only groups with at least 2 members.

query.sql
1SELECT department, COUNT(*) AS headcount
2FROM employees
3GROUP BY department
4HAVING COUNT(*) >= 2;
Result
departmentheadcount
Engineering3
Sales2
1Marketing is a singleton — dropped by the size rule.
2COUNT(*) >= 2 is the "hide empty/solo buckets" pattern.
3Useful for anonymisation too: suppress small groups.

WHERE + HAVING together

Rows filtered first, groups filtered second.

query.sql
1SELECT department, COUNT(*) AS headcount
2FROM employees
3WHERE salary >= 50000
4GROUP BY department
5HAVING COUNT(*) >= 2;
Result
departmentheadcount
Engineering3
Sales2
1WHERE drops David (47000) before bucketing.
2Marketing now has zero members — no bucket forms at all.
3HAVING >= 2 keeps Engineering and Sales; Marketing is absent, not zero.

HAVING without GROUP BY

The whole table becomes one group.

query.sql
1SELECT COUNT(*) AS n
2FROM employees
3HAVING COUNT(*) > 3;
Result
n
6
1No GROUP BY: all six rows are one group.
2HAVING checks that group's COUNT — 6 > 3 passes.
3If the table were empty, the group still exists (COUNT = 0) and the row would vanish.
--key facts
Memory hookWHERE filters rows · HAVING filters groups
Any aggregateHAVING COUNT(*) = 1 finds singleton groups instantly
Order fixedHAVING always follows GROUP BY, precedes ORDER BY
--use cases
"Departments spending more than X", "products with at least N reviews"
Anonymisation: show only groups with COUNT(*) >= 5
Top-group reports: HAVING SUM(amount) > 10000 ORDER BY SUM(amount) DESC
Debugging why a WHERE with an aggregate errors — the fix is always HAVING
--under the hood
HAVING cannot use indexes on aggregates by definition — it is evaluated after the grouping work.
Huge GROUP BY + HAVING queries get faster by narrowing with WHERE first: fewer rows to bucket, fewer buckets to test.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.