Find the second highest salary in SQL
This is one of the most asked SQL interview questions, because the obvious answer breaks on ties. Show one solution, then explain how it handles two people with the same salary.
Way 1: subquery with MAX
SELECT MAX(salary)FROM employeesWHERE salary < (SELECT MAX(salary) FROM employees);
Handles ties at the top correctly and returns NULL if there is no second salary.
Way 2: LIMIT and OFFSET
SELECT DISTINCT salaryFROM employeesORDER BY salary DESCLIMIT 1 OFFSET 1;
DISTINCT is essential. Without it, two people sharing the top salary make the "second" equal to the first.
Way 3: DENSE_RANK
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS r FROM employees) tWHERE r = 2;
Easy to change to the nth highest, and it works per department with PARTITION BY department.
What does the interviewer want to hear?
That you thought about ties, about what happens when there is only one employee, and about NULL salaries. Say which way you would use and why.
Try the SQL interview test Practise window functions
Updated November 2026.