SQL · Pattern 4 — Aggregation
Aggregate on Condition
Aggregate functions can follow a WHERE clause just like any other query — the filter narrows down which rows get aggregated, so the calculation only runs over the subset that matches.
Source data — Pattern 4: Aggregation
All queries run against the shared employees table (see schema.sql) — no new columns needed for this pattern.

What it does
Aggregate functions can follow a WHERE clause just like any other query — the filter narrows down which rows get aggregated, so the calculation only runs over the subset that matches.
Problem
Find the total salary of IT employees.
SELECT SUM(salary) AS it_total_salary
FROM employees
WHERE department_id = 100; -- 100 = IT
Result
