How to find the second-highest salary in SQL
Short answer
Use DENSE_RANK(): rank salaries from highest to lowest and keep rank 2. It handles ties and works for any Nth salary. Without window functions, use SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees).
On this page
Method 1: subquery with MAX
Find the highest salary, then the highest salary below it. Works in every SQL database.
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
If everyone earns the same, this returns NULL — which is usually what interviewers want.
Method 2: DISTINCT with LIMIT and OFFSET
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
DISTINCT matters: without it, two people on the top salary would make the "second" row the same top salary. This works in MySQL and PostgreSQL; SQL Server uses OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY.
Method 3: DENSE_RANK (best answer for interviews)
SELECT DISTINCT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) ranked
WHERE rnk = 2;
Change 2 to any N to get the Nth highest salary. DENSE_RANK gives tied salaries the same rank without gaps, so the answer stays correct when salaries repeat.
Method 4: second-highest salary in each department
SELECT department, salary
FROM (
SELECT department, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk
FROM employees
) ranked
WHERE rnk = 2;
Which one should you write in an interview?
- Start with DENSE_RANK and explain why it handles ties.
- Mention the MAX subquery as the version that runs on older databases.
- Say what happens when there is no second salary (the query returns no row or NULL).
Try it yourself
5 questions from the LogicByte practice bank. Pick an answer to check it.
-
1Which query selects the name and salary columns from Staff?
SELECT name, salary FROM Staff;Show answer
Answer: D — SELECT name, salary FROM Staff;
Separate multiple selected columns with commas.
-
2Which query labels the salary expression as annual_pay?
SELECT salary * 12 AS annual_pay FROM Employees;Show answer
Answer: D — SELECT salary * 12 AS annual_pay FROM Employees;
AS assigns a column alias to the calculated expression.
-
3Which query returns employees with salary greater than 50000?
SELECT * FROM Employees WHERE salary > 50000;Show answer
Answer: A — SELECT * FROM Employees WHERE salary > 50000;
Use WHERE salary > 50000 to filter employees by salary.
-
4Set 1 - Q05: How do you sort by salary from lowest to highest?
Show answer
Answer: C — ORDER BY salary ASC
ASC sorts values in ascending order.
-
5Set 1 - Q07: What does LIMIT 10 mean?
Show answer
Answer: B — Return at most 10 rows
LIMIT caps the number of rows returned.
Frequently asked questions
Why use DENSE_RANK instead of RANK?
RANK leaves gaps after ties (1, 1, 3), so rank 2 may not exist. DENSE_RANK has no gaps (1, 1, 2), so rank 2 is always the second-highest distinct salary.
How do I get the Nth highest salary?
Use the DENSE_RANK query and replace 2 with N, or use LIMIT 1 OFFSET N-1 with DISTINCT.
What if two employees share the highest salary?
Use DISTINCT or DENSE_RANK so the duplicate top salary is not counted as the second-highest.