Nth Highest Salary: Five Ways and the Traps

SQL Interview Problems: 12 Classic Questions Solved · Ranking and Top Rows · 5 min read · Updated

Finding the Nth highest

  1. Start with every row in the employees table.
  2. Remove rows where the salary is NULL, so they cannot confuse the order.
  3. Give each distinct salary a rank from the highest down, with no gaps.
  4. Keep only the rows whose rank equals N.
  5. The result is everyone at that salary, or nothing and then NULL if N is too big.

"Find the second highest salary" is probably the most asked SQL interview question in the world. It looks easy, and it is a good test because there are three hidden questions inside it. What does second highest mean when two people earn the same? What should the query return when there is no second highest? And can you also solve the Nth version, not only the second? This lesson answers all three.

Way 1: sort and skip with LIMIT and OFFSET

Sort the salaries from the highest to the lowest, skip the first one, and take the next one. The word DISTINCT matters: without it two people with the same top salary would make the "second" row equal to the first.

SELECT DISTINCT salary
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
salary
130000

The top salary is 150000 and the second highest is 130000. Notice the extra WHERE salary IS NOT NULL. Jack Reed has no salary. Different engines place NULL at the start or at the end of a sorted list, so the safe habit is to remove it yourself instead of depending on the default.

Way 2: the maximum below the maximum

SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
second_highest
130000

Read it from the inside: the subquery finds the highest salary, and the outer query finds the highest salary that is lower than that. It works in every database without special syntax, and it needs no sorting. Its weakness is that it only answers "second". For the third or fourth you would need to nest it again, which is ugly.

Way 3: DENSE_RANK, the answer that scales to N

WITH ranked AS (
  SELECT name, salary,
         DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees
  WHERE salary IS NOT NULL
)
SELECT name, salary
FROM ranked
WHERE rnk = 3;
name      | salary
Arjun Rao | 120000
Sara Khan | 120000

DENSE_RANK() OVER (ORDER BY salary DESC) gives rank 1 to the highest salary, rank 2 to the next different salary, and so on, without gaps. Asking for rank 3 returns everyone with the third highest salary. Here that is both Arjun and Sara, who earn the same 120000. To get the Nth, change the number. It returns every person at that level, which is usually what the question means by "who has the Nth highest salary".

ROW_NUMBER, RANK and DENSE_RANK are different

This is the follow-up question you should expect: what is the difference between the three? Run them side by side for the Engineering department.

SELECT name, salary,
       ROW_NUMBER() OVER w AS row_num,
       RANK()       OVER w AS rnk,
       DENSE_RANK() OVER w AS dense_rnk
FROM employees
WHERE dept_id = 1
WINDOW w AS (ORDER BY salary DESC);
name        | salary | row_num | rnk | dense_rnk
Meera Joshi | 150000 | 1       | 1   | 1
Nia Okoye   | 130000 | 2       | 2   | 2
Arjun Rao   | 120000 | 3       | 3   | 3
Sara Khan   | 120000 | 4       | 3   | 3
Liam Brown  | 95000  | 5       | 5   | 4
  • ROW_NUMBER never repeats. Arjun and Sara tie, but one of them gets 3 and the other 4, and which one is not defined unless you add a tie-breaker to ORDER BY.
  • RANK gives tied rows the same number and then skips. Both get 3, and the next person is 5, because places 3 and 4 were used.
  • DENSE_RANK gives tied rows the same number and does not skip. Both get 3, and the next person is 4.

For "Nth highest salary" use DENSE_RANK, because the Nth highest value is about distinct values. For "top 3 people" a different rule may apply, and you should ask the interviewer which one they mean.

When there is no answer

A good answer says what happens when the Nth value does not exist. A subquery that returns no row gives NULL when you wrap it as a scalar value, which is what many interviewers want. Here we ask for the 21st highest of 14 people.

SELECT (
  SELECT DISTINCT salary
  FROM employees
  WHERE salary IS NOT NULL
  ORDER BY salary DESC
  LIMIT 1 OFFSET 20
) AS twentyfirst_highest;
twentyfirst_highest
NULL

Without the outer SELECT ( ... ), the query would return no rows at all, and a program that reads the result may fail on the empty set. Say which behaviour you chose and why.

Common mistakes

  • Forgetting DISTINCT in the LIMIT version, so ties return the top salary again.
  • Using MAX(salary) ... WHERE salary < MAX(salary) in the same query level, which is not allowed because the aggregate is not known yet during WHERE.
  • Not handling NULL salaries.
  • Using ROW_NUMBER and silently choosing one of two tied people.

Your task

  1. Find the third highest distinct salary using LIMIT and OFFSET. Check that it matches the DENSE_RANK answer.
  2. Change the DENSE_RANK query to return the 2nd highest salary and the person or people who earn it.
  3. Find the second lowest salary. Decide what you do about Jack Reed, who has no salary, and say why.
  4. Write the query so it returns NULL when the Nth salary does not exist.

How to check it worked

  • The second highest salary is 130000.
  • The third highest salary returns two people, Arjun Rao and Sara Khan, with 120000.
  • Your second lowest query does not return NULL from Jack Reed's missing salary.

Frequently asked questions

Is LIMIT with OFFSET or DENSE_RANK better?

DENSE_RANK is clearer, handles ties as you expect and works for any N. LIMIT with OFFSET is short and fine for a quick check, but it only returns the salary, not the person.

Why does the interviewer ask about ties?

Because real data has ties, and a query that ignores them gives wrong answers quietly. Showing that you ask about them shows work experience thinking.

What does the same question look like in MySQL or SQL Server?

The window function version works in MySQL 8, PostgreSQL and SQL Server. For the LIMIT version, SQL Server uses OFFSET and FETCH, and Oracle uses FETCH FIRST.