Get nth Highest and Lowest Salary of an Employee
Estimated study time: 3 minutes. A classic SQL interview question, solved three ways.
Finding the nth highest or lowest salary is one of the most common SQL interview questions, because it tests whether you understand subqueries, ranking functions, and pagination-style logic all at once.
Method 1: Using a Subquery
SELECT MIN(salary) FROM ( SELECT TOP 3 salary FROM employees ORDER BY salary DESC ) AS top_salaries;
This finds the 3rd highest salary by taking the top 3 salaries, then picking the smallest of that group.
Method 2: Using DENSE_RANK()
SELECT salary FROM ( SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees ) ranked WHERE rnk = 3;
DENSE_RANK() is the safest choice when duplicate salaries exist, since it won't skip ranks the way ROW_NUMBER() can behave unexpectedly with ties.
Method 3: Using OFFSET-FETCH
SELECT salary FROM employees ORDER BY salary DESC OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY;
Finding the Nth Lowest Salary
Simply reverse the sort order to ASC in any of the three methods above.
DENSE_RANK() when you want tied salaries to count as one rank; use ROW_NUMBER() when every row should be ranked individually regardless of ties.Why Interviewers Ask This
It reveals whether a candidate understands ordering, ranking, and pagination logic — skills used constantly in real-world reporting queries.
Writing the Same Queries in MySQL
The methods above use SQL Server syntax such as TOP and OFFSET ... FETCH. MySQL uses LIMIT and OFFSET instead, and supports window functions like DENSE_RANK() from MySQL 8 onward.
-- 3rd highest distinct salary
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
-- return NULL instead of an empty result when there is no 3rd salary
SELECT (
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2
) AS third_highest;
The DISTINCT keyword matters. Without it, two employees on the same salary take up two positions and the query returns the wrong number. The same issue affects the TOP 3 subquery approach, which should also select distinct salaries.
How Ties Change the Answer
Consider five salaries: 90,000, 85,000, 85,000, 70,000 and 60,000. The three ranking functions number them differently:
| Salary | ROW_NUMBER() | RANK() | DENSE_RANK() |
|---|---|---|---|
| 90,000 | 1 | 1 | 1 |
| 85,000 | 2 | 2 | 2 |
| 85,000 | 3 | 2 | 2 |
| 70,000 | 4 | 4 | 3 |
| 60,000 | 5 | 5 | 4 |
Ask for "rank 3" and you get 85,000 from ROW_NUMBER, nothing at all from RANK, and 70,000 from DENSE_RANK. Most interviewers mean the third distinct salary, which is 70,000, so DENSE_RANK is the usual answer. Saying this out loud in an interview shows you understand the edge case.
Nth Highest per Department
A common follow-up is to find the second highest salary in each department. PARTITION BY restarts the ranking for every department:
WITH ranked AS (
SELECT department_id, name, salary,
DENSE_RANK() OVER (PARTITION BY department_id
ORDER BY salary DESC) AS rnk
FROM employees
)
SELECT department_id, name, salary
FROM ranked
WHERE rnk = 2;
Swap DESC for ASC to get the second lowest in each department.
A Version Without Window Functions
Older MySQL versions can use a correlated subquery. A salary is the Nth highest when exactly N minus one distinct salaries are greater than it:
SELECT DISTINCT e1.salary
FROM employees e1
WHERE 2 = (SELECT COUNT(DISTINCT e2.salary)
FROM employees e2
WHERE e2.salary > e1.salary);
This finds the 3rd highest. It is easy to explain but slower on large tables, because the inner query runs for each row.
Small Things That Trip People Up
- NULL salaries: they sort first in ascending order in MySQL, so add
WHERE salary IS NOT NULLwhen finding the lowest. - LIMIT with a calculation: MySQL does not accept
LIMIT 1 OFFSET N-1with an expression. Inside a stored function, compute the offset into a variable first. - Performance: an index on
salaryhelps the ORDER BY versions considerably on big tables. - Returning the employee, not just the number: once you know the salary value, join back to the table or select
nameinside the ranked query, since several people may share that salary. - Clarify the question first: ask whether ties should count once or separately. The right function depends entirely on that answer.