Revenue and Headcount Per Department
Return one row per department with:
department— the department nameheadcount— how many employees it hasrevenue— the totalamountof its orders
Every department must appear, including any with no employees or no orders.
Report 0 and 0.00 rather than NULL.
Order by department.
A warning. Joining department to both
employee and "order" in one FROM clause
gives wrong numbers for both columns. Engineering's real revenue is
6000.00; that query reports 18000.00.
Expected output
department | headcount | revenue
------------+-----------+--------
Engineering | 3 | 6000.00
Research | 0 | 0.00
Sales | 1 | 750.00
Support | 2 | 0.00
Editorial
Run the starter query. Engineering comes back with
headcount 9 and revenue 18000.00, and nothing about
the query looks wrong.
What actually happened
Joining one department row to 3 employees gives 3 rows. Joining each of those to 3 orders gives 9. The join produced the Cartesian product of the two child sets, because nothing relates an employee to an order — they are siblings, not a chain. Every employee is now paired with every order:
Alice-1000 Alice-2000 Alice-3000
Bob-1000 Bob-2000 Bob-3000
Carol-1000 Carol-2000 Carol-3000
sum(o.amount) over those 9 rows is 18000, which is the real
6000 times the headcount. count(e.id) is 9, which is the real 3
times the order count. Each aggregate is inflated by the other branch's
row count. This is fan-out, and the giveaway is that both numbers are wrong by
a suspiciously round factor.
count(DISTINCT e.id) fixes the headcount, and people reach for
it. It does not fix the revenue, and it cannot: two orders of 1000.00 are
genuinely two orders, so sum(DISTINCT o.amount) would quietly
collapse them into one and report 1000 where the answer is 2000. Distinct
repairs a count and corrupts a sum.
Aggregate first, then join
WITH heads AS (
SELECT department_id, count(*) AS headcount
FROM employee GROUP BY department_id
), rev AS (
SELECT department_id, sum(amount) AS revenue
FROM "order" GROUP BY department_id
)
SELECT d.name AS department,
coalesce(h.headcount, 0) AS headcount,
coalesce(r.revenue, 0.00) AS revenue
FROM department d
LEFT JOIN heads h ON h.department_id = d.id
LEFT JOIN rev r ON r.department_id = d.id
ORDER BY d.name;
Each CTE collapses its table to one row per department before the join happens. Joining one row to one row cannot fan out. This is the general fix and it is worth holding as a rule: when one row needs aggregates from two independent children, aggregate each child separately and join the results.
The scalar subquery version
SELECT d.name AS department,
(SELECT count(*) FROM employee e WHERE e.department_id = d.id) AS headcount,
(SELECT coalesce(sum(amount), 0.00) FROM "order" o WHERE o.department_id = d.id) AS revenue
FROM department d
ORDER BY d.name;
Also correct, and shorter. Two differences worth knowing. It handles the
empty case for free — a scalar count over no rows is 0, no
coalesce needed — but sum still returns NULL, so one
coalesce survives. And the planner usually rewrites it into
roughly the CTE plan anyway, so the choice is about readability rather than
speed. At four or five such columns the CTE version stays readable and the
subquery version does not.
Research and Support
Research has no employees and no orders, so it is absent from both CTEs and
only survives because the joins are LEFT. Support has employees but
no orders, so it appears in one CTE and not the other. A single department with
one of these shapes is what turns an INNER JOIN here from "works"
into "drops rows nobody notices".
What an interviewer is checking
Whether you notice the grain. The question "what is one row of this join" is the one that catches fan-out every time, and the strong answer names the problem before writing the fix. The usual follow-up is "now add average order value", which is the same trap again — and the point at which the CTE version stops being a stylistic preference.
department
4 rows
| idinteger | nametext |
|---|---|
| 1 | Engineering |
| 2 | Sales |
| 3 | Support |
| 4 | Research |
employee
6 rows
| idinteger | nametext | department_idinteger |
|---|---|---|
| 1 | Alice | 1 |
| 2 | Bob | 1 |
| 3 | Carol | 1 |
| 4 | Dan | 2 |
| 5 | Erin | 3 |
Showing 5 of 6 rows.
order
5 rows
| idinteger | department_idinteger | amountnumeric(10,2) |
|---|---|---|
| 1 | 1 | 1000 |
| 2 | 1 | 2000 |
| 3 | 1 | 3000 |
| 4 | 2 | 500 |
| 5 | 2 | 250 |