1.What is the difference between WHERE and HAVING?
Warm-upWhat a strong answer covers
- WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY has aggregated them.
- Aggregate functions (COUNT, SUM, AVG) can appear in HAVING but not in WHERE, because aggregates don't exist yet at row-filtering time.
- Logical evaluation order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
- Performance note: filtering early in WHERE shrinks the working set before the (more expensive) grouping step.
Where people lose the point
- Saying they're interchangeable, or that HAVING is just 'WHERE for GROUP BY' without mentioning aggregates.
- Putting an aggregate condition in WHERE and expecting it to work.
- Not knowing the logical clause evaluation order when pressed on why.