All problems

Who's the Runner-Up in Pay?

hardSQLSubqueriesSorting

Payroll at Kestrel Labs is benchmarking the top of the pay scale against a market survey, and the founder's salary distorts the picture. What the analyst actually asked for is the person on the next rung down: the runner-up across the whole company, not the runner-up on any one team.

Finding the single highest salary is a one-move question. The runner-up is not, because "second" is a position rather than a value — there is no column you can filter on that says so. And a definition has to be pinned before the query can be written: if two people were tied at the top, does the runner-up mean the second of those two, or the next person below the shared figure? The two readings give different answers, and the report wants the first one.

employees — one row per person. 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 exactly one row with two columns, name and salary, for the second-best-paid employee in the company. "Second" means positional: arrange everybody best paid first and take the person standing in the second position. So if two people shared the top salary, this returns the second of that pair — still on the top figure — rather than the next lower salary.

Example output — shape only, on an invented payroll.

name salary
Yusuf Bello 138500

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…