All problems

Who's Earning Above Their Team's Average?

mediumSQLSubqueries

Comp & Benefits at Kestrel Labs opens the annual review cycle next week, and the first pass is a fairness check: on every team, who is already paid more than that team typically pays?

The awkward part is that there is no single figure to measure people against. Engineering pay and Marketing pay are set by two different markets, so one company-wide number would flag most of Engineering and nobody in Marketing — which tells People Ops nothing they can act on. Each person has to be held up against their own team's typical pay, and that typical pay is not stored anywhere. It has to come out of the same ten rows you are reading.

employees — one row per person. salary is the annual figure in whole dollars. department_id is the team the person sits 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 a single column, name, holding every employee whose salary is strictly above the average salary of their own department. Somebody paid exactly their team's average is not above it and does not appear. The only person on a one-person team can never be above their own average, so that team contributes nobody. Hand the names back in alphabetical order.

Example output — shape only, on invented names.

name
Anton Reis
Mira Kovac
Rosa Kwan

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…