Solve “Departments with more than three people”

Free. Runs in your browser, no install.

SQL · Basics & Joins

WHERE vs HAVING

Easywherehavingaggregation

Interview question

What is the difference between WHERE and HAVING?

How to answer

The short answer

WHERE filters rows before grouping.

HAVING filters groups after GROUP BY.

SQL
SELECT department, COUNT(*)
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING COUNT(*) > 10;

Rough execution order:

Output
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY

You cannot put aggregates like `COUNT()` in WHERE, because grouping has not happened yet.

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill