AnalystPracticeGuides
DashboardSign up

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.

Watch it solved. Q7: the second highest salary, from 4:30, in our video “SQL Interview Questions and Answers for Data Analysts (Easy to Hard)”. Pressing play loads the video from YouTube (privacy). Open on YouTube

Way 1: subquery with MAX

SELECT MAX(salary)
FROM employees
WHERE 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 salary
FROM employees
ORDER BY salary DESC
LIMIT 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
) t
WHERE 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 it yourself. Practise this in a real database in your browser, with instant feedback.
Try the SQL interview test Practise window functions

Updated November 2026.

← All free guides