Skip to content
We areTOP 1%

Second Highest Salary

MediumLeetCodeSQL: Subqueries & CTEs
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Hint and exampleShow

Take the largest value that is strictly smaller than the overall maximum; MAX over no rows gives NULL, which handles the case where no second value exists.

Example: runs(player, score)

player | score
A      | 90
B      | 75
C      | 90
D      | 60
SELECT MAX(score) AS second_best
FROM runs
WHERE score < (SELECT MAX(score) FROM runs);   -- returns 75
-- if every score were 90, the result would be NULL

More in SQL: Subqueries & CTEs

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account