SQL · Pattern 5 — Grouping
GROUP BY Multiple Columns
Grouping by more than one column creates one group per unique combination of those columns, not one group per column.
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
Grouping by more than one column creates one group per unique combination of those columns, not one group per column. Two employees in the same department but different cities land in different groups — grouping by department alone would have merged them.
Problem
Find average salary by department and city.
SELECT department_id,
city,
AVG(salary) AS average_salary
FROM employees
GROUP BY department_id, city;
Result
