The short answer
WHERE filters rows before grouping.
HAVING filters groups after GROUP BY.
SELECT department, COUNT(*)
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING COUNT(*) > 10;Rough execution order:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BYYou cannot put aggregates like `COUNT()` in WHERE, because grouping has not happened yet.