All problems

Where Does Everyone Rank on Their Team?

hardSQLWindow Functions

Promotion season has opened at Kestrel Labs, and every manager has asked People Ops for the same sheet. Not what the team is paid — a manager reviewing one team has no business reading another team's figures — but how each person sits against the colleagues beside them. So the pay itself stays off the page. What goes on it is a standing.

One company-wide list would not do. Engineering pay and Marketing pay are set by two different markets, so the best-paid person in Marketing lands mid-table on an all-company list and looks ordinary. The comparison has to start over at every team boundary, and nobody may be dropped on the way: a sheet that quietly loses the sole member of a small team is worse than no sheet.

employees — one row per person. department_id is the team they sit on. manager_id points at another employee's id and is NULL for the people who lead a team.

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 one row for every employee, with name, department_id, and salary_rank — that person's standing by pay inside their own department, 1 being the best paid on that team. Two teammates paid exactly the same share a number, and the number straight after it is skipped, so a team can read 1, 2, 2, 4. Everybody comes back, including the only member of a one-person team, who is a 1. Rows come back sorted by department_id, then by salary_rank, then by name — three keys, because the first two together can still leave two teammates tied.

Example output — shape only, on an invented company; the two teammates sharing position 2 show the shared number and the skip that follows it.

name department_id salary_rank
Anton Reis 5 1
Lena Fournier 5 2
Mira Kovac 5 2
Rosa Kwan 6 1

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

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…