Guided Notes

GROUP BY, HAVING & UNION

One summary row per group with GROUP BY, filtering groups with HAVING, WITH ROLLUP totals, and stacking results with UNION.

GROUP BY

Slide 1 / 5

GROUP BY splits rows into groups and computes aggregates for each:

SELECT category, COUNT(*) AS products, ROUND(AVG(price), 2) AS avg_price FROM products GROUP BY category;

Rule: every selected column must either be in GROUP BY or be inside an aggregate. MySQL's default ONLY_FULL_GROUP_BY mode enforces this — and it saves you from confusing results.

← Joins