HAVING and Evaluation Order
The clauses do not run in the order they are written. This one fact explains most confusing SQL errors.
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
WHERE runs before grouping, so it cannot see an aggregate: WHERE COUNT(*) > 2 is an error, because at that point there are no groups yet. HAVING is the clause that filters groups, after they exist.
SELECT runs near the end, which is why a column alias you define there cannot be used in WHERE, but can be used in ORDER BY. That asymmetry surprises everyone exactly once.
Mark this lesson complete
Signed in, your progress follows you to every device.