How does WHERE differ from HAVING?
Short answer
WHERE filters individual rows before grouping, so it cannot use aggregate functions. HAVING filters groups after GROUP BY, so conditions on COUNT, SUM, AVG go there.
3 points to say:
WHEREfilters rows before grouping and cannot use aggregates.HAVINGfilters groups afterGROUP BY; put plain conditions inWHEREto use indexes and filter early.- Every
SELECTcolumn must be inGROUP BYor aggregated.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.