Conceptual
Login

WHERE Filters Rows Before Grouping and HAVING Filters Groups After

WHERE is applied to individual rows before they are grouped; HAVING is applied to the groups after the aggregates have been computed. That means a condition that does not depend on an aggregate belongs in WHERE, where it throws rows away before the expensive grouping step, and putting it in HAVING makes the engine group rows it is about to discard. Only conditions on aggregate values, such as HAVING COUNT(*) > 5, genuinely need HAVING.

Questions this Concept answers

  • Why does expressing a non-aggregate condition in `WHERE` rather than `HAVING` describe less work?