GROUP BY is the heart of data analysis in SQL. It collapses rows into groups so you can compute totals, averages, and counts per category — the exact logic behind every summary report and dashboard tile.
Key Points
- Basic pattern: SELECT department, COUNT(*) FROM employees GROUP BY department;
- Combine with aggregates: SUM(sales), AVG(salary), MAX(order_date)
- Filter groups with HAVING: HAVING COUNT(*) > 5
- Every non-aggregated column in SELECT must appear in GROUP BY
- Group by multiple columns for cross-tab style summaries
- WITH ROLLUP adds grand totals in one query
.png)