Module lessons (4/4)
Filtering Groups: HAVING
Keep only groups whose aggregate result satisfies a condition.
What you'll learn in this lesson
- Filter already aggregated groups with
HAVING, including the time difference compared toWHERE. - Combine both filters (
WHEREbeforeGROUP BY,HAVINGimmediately after) into a single analytic query. - Know why PostgreSQL allows aliases in
ORDER BYbut not insideHAVING.
Overview
Once you have calculated counts or averages per department, you often want to keep only the groups above the threshold (e.g. departments with at least 2 people or countries with an average above a target). This is why WHERE is not enough: when WHERE runs, the groups do not yet exist. This is where HAVING comes into play.
Advance step by step in the preset scenario WHERE vs HAVING: observe Evan rejected already at step WHERE (individual row), then Design rejected at step HAVING (entire group below average 5000!):
Sections covered in this lesson
- 11. Mental Model: WHERE (Rows) vs HAVING (Groups)
- 22. Progressive & Annotated Examples
- 33. Interactive Laboratory: WHERE vs HAVING Live
- 44. Diagnostic Verification (Quiz)
- 55. Progressive Practical Laboratory
Unlock "Filtering Groups: HAVING" and the full SQL Course curriculum
Keep only groups whose aggregate result satisfies a condition.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in SQL Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.