SQL GROUP BY and HAVING Tutorial: COUNT, SUM, AVG, ROLLUP and Pivot Reports in MySQL
Totals, averages and counts are the heart of almost every report: sales per branch, students per course, orders per day, the best-selling product this week. In SQL, all of these come from one clause: GROUP BY. Paired with aggregate functions like COUNT, SUM and AVG, and filtered with HAVING, it turns thousands of rows into a short summary you can show on a dashboard.