SQL · Pattern 5 — Grouping
GROUP BY
GROUP BY collapses rows that share a value into one row per group, and any aggregate function in the SELECT list (SUM, AVG, COUNT...) then runs per group instead of over the whole table.
Source data — Pattern 5: Grouping
All queries run against the shared employees table (see schema.sql). This pattern needs two new columns — city (topic 30) and job_role (topic 31) — and enough rows in one department to actually cross a "more than 5" threshold (topic 29), so IT grows from 2 employees to 7.

What it does
GROUP BY collapses rows that share a value into one row per group, and any aggregate function in the SELECT list (SUM, AVG, COUNT...) then runs per group instead of over the whole table.
Problem
Find total salary by department.
SELECT department_id,
SUM(salary) AS total_salary
FROM employees
GROUP BY department_id;
Result
