GROUP BY with HAVING; MySQL strict mode requires non-aggregated SELECT columns to appear in GROUP BY.
Aggregation
SELECT status, COUNT(*) AS cnt, SUM(total) AS revenue
FROM orders
GROUP BY status
HAVING SUM(total) > 1000;Practice: Run on practice.
ONLY_FULL_GROUP_BY
Invalid selects like SELECT status, customer_id, COUNT(*) without grouping customer_id fail in strict mode—fix query or use ANY_VALUE() deliberately.
Important interview questions and answers
- Q: HAVING vs WHERE?
A: WHERE filters rows before group; HAVING filters groups after aggregation. - Q: COUNT(*) vs COUNT(col)?
A: COUNT(col) ignores NULLs in col.
Self-check
- Revenue by status over 1000 total.
- Why ONLY_FULL_GROUP_BY?
Pitfall: SELECT col, COUNT(*) without GROUP BY col fails in strict mode.
Interview prep
- HAVING?
Filters groups after aggregation.
- COUNT(*)?
Counts rows including NULLs in other columns.