Skip to content
Reference notes from the interactive SQL course, which covers the same ground with a live SQL console, graded challenges and a final exam.
Grouping & Aggregation

HAVING — Filtering Groups

WHERE filters rows, HAVING filters groups, and the difference changes answers.

HAVING — Filtering Groups

WHERE throws away rows before grouping. HAVING throws away groups after.

SQL
SELECT dept_id, COUNT(*) AS staff
FROM employees
GROUP BY dept_id
HAVING COUNT(*) >= 3
ORDER BY staff DESC;

All four departments are grouped; only those with three or more people survive. WHERE COUNT(*) >= 3 would be an error — at WHERE time no group exists yet, so there is nothing to count.

Where each one sits

FROM      → assemble rows
WHERE     → drop rows            ← individual rows
GROUP BY  → form groups
HAVING    → drop groups          ← whole buckets
SELECT    → compute columns
ORDER BY  → sort

Using both, which is the normal case

SQL
SELECT dept_id,
       COUNT(*)              AS active_staff,
       ROUND(AVG(salary), 2) AS avg_salary
FROM employees
WHERE active = 1
GROUP BY dept_id
HAVING COUNT(*) >= 2
ORDER BY avg_salary DESC;

Read it in execution order: keep active employees, group them by department, keep departments with at least two of them, then compute and sort.

The distinction that changes the answer

These two queries look almost identical and do not mean the same thing:

SQL
SELECT dept_id, ROUND(AVG(salary), 2) AS avg_of_high_earners
FROM employees
WHERE salary > 70000
GROUP BY dept_id
ORDER BY dept_id;
SQL
SELECT dept_id, ROUND(AVG(salary), 2) AS dept_avg
FROM employees
GROUP BY dept_id
HAVING AVG(salary) > 70000
ORDER BY dept_id;

The first averages only the salaries above 70,000, department by department. The second averages everyone and then keeps departments whose average clears 70,000. Different questions, different numbers, and both are valid SQL — so nothing tells you which one you meant except reading it carefully.

The rule of thumb: if the condition is about an individual row, it belongs in WHERE. If it is about the group as a whole, it belongs in HAVING.

HAVING without GROUP BY

Legal, and occasionally useful: the whole table is treated as one group.

SQL
SELECT COUNT(*) AS orders, ROUND(SUM(amount), 2) AS revenue
FROM orders
HAVING SUM(amount) > 100;

The row appears because total revenue clears 100. Had it not, you would get zero rows — which is a neat way to say "give me this summary only if it matters", and a common trick in alerting queries.

Put row conditions in WHERE, not HAVING

Most row-level conditions can be written in HAVING when the column is in the GROUP BY, and it is a bad habit:

SQL
SELECT dept_id, COUNT(*) AS staff
FROM employees
GROUP BY dept_id
HAVING dept_id = 1;

This works and returns the right answer, but it groups all ten employees and then discards three of the four buckets. WHERE dept_id = 1 groups four rows and no more. On a table with millions of rows that is the difference between a fast query and a slow one, and it is a common review comment.

Aliases in HAVING

Standard SQL does not let HAVING use a SELECT alias — HAVING staff >= 3 fails on PostgreSQL and Oracle, because HAVING runs before SELECT. MySQL and SQLite permit it as an extension. Repeat the aggregate to stay portable.

Common mistakes

  • An aggregate in WHERE — the error that sends people here.
  • A row condition in HAVING — correct but wasteful, and it hides intent.
  • Confusing "average of the filtered rows" with "filter on the average" — the two queries above.
  • Relying on aliases in HAVING — works on MySQL, fails on Postgres.

Interview question

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping and cannot see aggregates; HAVING filters groups after grouping and is where aggregate conditions go. The follow-up worth preparing: give an example where moving a condition between them changes the result — the two queries above are exactly that.

Check yourself

  1. Find customers who have placed more than one order.
  2. Rewrite HAVING dept_id = 2 as a WHERE and say why it is better.
  3. Why does HAVING staff >= 3 fail on PostgreSQL when staff is a SELECT alias?
All 43 lessons in SQL — The Interactive Visual Notebook
  1. Introduction to SQL
  2. Why SQL Matters
  3. What is an RDBMS?
  4. Tables, Rows, and Columns
  5. SQL Command Categories (DDL/DML/DCL/DQL)
  6. SELECT and FROM
  7. Aliases and Expressions
  8. DISTINCT and Sorting
  9. Limiting Results
  10. Query Execution Order
  11. WHERE and Comparison Operators
  12. Combining Conditions — AND, OR, NOT
  13. IN, BETWEEN, and LIKE
  14. Working with NULL
  15. Aggregate Functions
  16. GROUP BY
  17. HAVING — Filtering Groups
  18. Grouping in Practice
  19. INNER JOIN
  20. LEFT JOIN and Outer Joins
  21. Self Joins
  22. Cross Joins and Join Pitfalls
  23. UNION, INTERSECT, and EXCEPT
  24. Subqueries
  25. EXISTS and Correlated Subqueries
  26. Common Table Expressions (WITH)
  27. Recursive CTEs
  28. Window Functions — The OVER Clause
  29. Ranking Functions
  30. Frames and Running Totals
  31. LAG and LEAD
  32. Window Function Patterns
  33. What an Index Is
  34. Reading a Query Plan
  35. Designing Indexes
  36. Writing Queries an Index Can Use
  37. Transactions and ACID
  38. Isolation Levels
  39. Locks and Deadlocks
  40. Choosing Data Types
  41. Constraints and Keys
  42. Normalisation
  43. Designing a Schema