HAVING vs WHERE
Filtering before grouping versus after, and which clause sees the aggregate.
The Question You Will Get
'What is the difference between WHERE and HAVING?' is one of the most asked SQL interview questions. A weak answer says 'HAVING is for aggregates.' A strong answer explains when each clause runs in the query pipeline.
That timing is the whole story: WHERE filters rows before grouping; HAVING filters groups after aggregation.
Where They Sit in Execution Order
Recall the logical execution order of a query:
FROM/JOIN→ build the row setWHERE→ filter individual rowsGROUP BY→ collapse into groupsHAVING→ filter the groupsSELECT→ project columnsORDER BY→ sort
WHERE happens before groups exist; HAVING happens after, so HAVING can see aggregates and WHERE cannot.