Skip to main content
Module 3 · Lesson 4 of 412/57 in the course~12 min
Module lessons (4/4)

Filtering Groups: HAVING

Lesson preview5 hands-on exercises readySQL Course · Module 3 — JOIN and GROUP BY

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 to WHERE.
  • Combine both filters (WHERE before GROUP BY, HAVING immediately after) into a single analytic query.
  • Know why PostgreSQL allows aliases in ORDER BY but not inside HAVING.

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

  1. 11. Mental Model: WHERE (Rows) vs HAVING (Groups)
  2. 22. Progressive & Annotated Examples
  3. 33. Interactive Laboratory: WHERE vs HAVING Live
  4. 44. Diagnostic Verification (Quiz)
  5. 55. Progressive Practical Laboratory
Pro Lesson · Module 3 — JOIN and GROUP BY

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.