Which Team Has the Widest Pay Gap?
An external pay-equity audit has landed at Kestrel Labs, and the first thing the auditors want is a crude but revealing figure: for each team, the distance between its best-paid and its worst-paid person. A wide distance is not proof of anything on its own, but it tells the auditors which teams to open up first.
The awkward part is that the answer for a team depends on two different rows of the employee table at once — and which two is not known in advance. Marketing is a further wrinkle: it has exactly one employee, so its best-paid and worst-paid person are the same human being. The audit still expects Marketing to appear on the report rather than quietly falling off it.
employees — one row per person. department_id is the team they sit on. salary is annual pay in dollars.
| id | name | department_id | salary | hire_date | manager_id |
|---|---|---|---|---|---|
| 1 | Ava Chen | 1 | 145000 | 2021-03-14 | NULL |
| 2 | Ben Ortiz | 1 | 118000 | 2022-06-01 | 1 |
| 3 | Cara Novak | 1 | 121000 | 2023-01-10 | 1 |
| 4 | Deshawn Lee | 2 | 95000 | 2020-09-23 | NULL |
| 5 | Elin Kask | 2 | 88000 | 2022-11-05 | 4 |
| 6 | Farid Amiri | 3 | 76000 | 2023-04-18 | NULL |
| 7 | Grace Kim | 4 | 132000 | 2021-07-30 | NULL |
| 8 | Hugo Silva | 4 | 110000 | 2023-02-14 | 7 |
| 9 | Ines Duarte | 4 | 104000 | 2023-08-01 | 7 |
| 10 | Jonas Weber | 2 | 91000 | 2021-12-19 | 4 |
The table already exists in the database — there is nothing to create or load.
Task: Write a query returning two columns, department_id and salary_spread, with one row per department that has at least one employee. salary_spread is the highest salary on that team minus the lowest, in whole dollars. A team of one produces a row with a spread of 0, not an absent row. A department with no employees at all produces no row. Rows come back sorted by department_id, smallest first.
Example output — shape only, on an invented company; team 6 is a team of one, which is why it reads 0 rather than being missing.
| department_id | salary_spread |
|---|---|
| 5 | 41000 |
| 6 | 0 |
| 8 | 12500 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in