HAVING
HAVING is WHERE for groups. SELECT city, AVG(marks) FROM students GROUP BY city HAVING AVG(marks) < 50; cities whose average is below 50.
You cannot put AVG(marks) in WHERE because WHERE runs before groups exist. Remember: WHERE rows → GROUP BY → HAVING groups → ORDER BY.
Trap: HAVING without GROUP BY on a detail query just to look clever. Trap: repeating the same AND in WHERE and HAVING without knowing which stage it is.
On the example next to this theory — HAVING — gROUP BY aggregates; HAVING filters groups; ORDER BY sorts the result.
Exam tip
GROUP BY city HAVING AVG(marks) < 50;