Sales Per Employee Including the Quiet Ones
For every employee return their name, the number of sales they
closed as sales, and the total value of those sales as
total.
Every employee must appear, including the ones who closed nothing. For those
employees report 0 sales and a total of
0.00 — not NULL.
Order by name.
Expected output
name | sales | total
------+-------+--------
Alice | 3 | 1950.50
Bob | 1 | 9000.00
Carol | 2 | 100.00
Dan | 0 | 0.00
Erin | 0 | 0.00
Editorial
Three decisions, and each one has a plausible wrong answer.
SELECT e.name,
count(s.id) AS sales,
coalesce(sum(s.amount), 0.00) AS total
FROM employee e
LEFT JOIN sale s ON s.employee_id = e.id
GROUP BY e.id, e.name
ORDER BY e.name;
LEFT, not INNER
An INNER JOIN returns three rows. Dan and Erin have no matching
sale, so there is nothing to pair them with and they vanish. The statement asks
for every employee, so the preserved side has to be the employee table.
count(s.id), not count(*)
This is the one that catches people. After the LEFT JOIN, Dan
has exactly one row — himself, with every sale column NULL.
count(*) counts rows, so it reports 1 for
an employee who sold nothing:
-- WRONG: Dan and Erin come back with 1 sale each
SELECT e.name, count(*) AS sales
FROM employee e
LEFT JOIN sale s ON s.employee_id = e.id
GROUP BY e.id, e.name;
count(<expression>) counts rows where the expression is
not NULL, so count(s.id) reports 0. The rule worth memorising:
over an outer join, count a column from the inner side, never
*.
coalesce around sum, not around the column
sum() over no non-NULL input returns NULL, not zero. That is
standard and it surprises everyone once. The fix goes outside the
aggregate:
coalesce(sum(s.amount), 0.00) -- right
sum(coalesce(s.amount, 0.00)) -- also gives 0.00 here, by accident
The second one happens to work because the only NULLs come from the join. It
stops working the moment amount is itself nullable and you wanted
NULL amounts excluded rather than treated as zero. Put the
coalesce outside and the intent is unambiguous.
GROUP BY e.id, e.name
Grouping by e.name alone would be wrong the day two employees
share a name — their rows would be merged. Grouping by the primary key is
correct, and PostgreSQL then lets you select e.name without
listing it, because a primary key functionally determines the rest of its row.
Listing both is habit worth keeping: it is portable, and it survives someone
later swapping the key.
What an interviewer is checking
Whether you volunteer the zero rows without being prompted. "Include the ones
with none" is the standard follow-up to every per-group count, and reaching for
count(*) over an outer join is the standard way to get it wrong.
employee
5 rows
| idinteger | nametext |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
| 4 | Dan |
| 5 | Erin |
sale
6 rows
| idinteger | employee_idinteger | amountnumeric(10,2) |
|---|---|---|
| 1 | 1 | 1200 |
| 2 | 1 | 450.5 |
| 3 | 1 | 300 |
| 4 | 2 | 9000 |
| 5 | 3 | 75.25 |
Showing 5 of 6 rows.