All problems

Second Highest Salary

mediumSQLSubqueriesRanking

Kestrel Labs hands out one runner-up equity grant a year and, by a rule nobody enjoys explaining, it goes to the second-best-paid person in the company — the top earner already has a package of their own. People Ops needs the name and the figure to put in the letter.

Nothing in the table marks second place. Pay is just a column of numbers, and the person who is second today may be fourth after the next review, so the position has to be worked out from the salaries every time the query runs.

employees — one row per employee. salary is annual, in dollars. manager_id points at another employee, or is NULL.

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, name and salary, for the employee with the second-highest salary company-wide. Exactly one row.

Example output — shape only, on an invented payroll.

name salary
Yusuf Bello 139000

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…