How to Find the Nth Highest Salary in SQL (3 Methods)
Find the 2nd, 3rd, or Nth highest salary in SQL. Covers DENSE_RANK, LIMIT OFFSET, correlated subquery methods. Classic interview question with working examples.
Introduction
Finding the Nth highest salary is one of the most common SQL interview questions. There are three main approaches, each with different trade-offs.
Method 1: DENSE_RANK Window Function (Best)
The most reliable method. DENSE_RANK handles ties correctly — if two people have the same salary, they get the same rank.
-- Find the 3rd highest salary:
SELECT salary FROM (
SELECT salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
) ranked
WHERE rank = 3;
-- Find the Nth highest (parameterized):
SELECT DISTINCT salary FROM (
SELECT salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
) ranked
WHERE rank = @N; -- Replace @N with any number
Tip: Use DENSE_RANK, not RANK or ROW_NUMBER. DENSE_RANK gives consecutive ranks even with ties (1,2,2,3). RANK would give (1,2,2,4).
Method 2: LIMIT OFFSET (MySQL & PostgreSQL)
Use DISTINCT + ORDER BY + LIMIT/OFFSET for a simpler approach.
-- MySQL: 3rd highest salary (N-1 = 2):
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
-- PostgreSQL (same syntax):
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
-- SQL Server: use TOP with subquery:
SELECT MIN(salary) FROM (
SELECT DISTINCT TOP 3 salary
FROM employees
ORDER BY salary DESC
) AS top_salaries;
Tip: OFFSET is N-1. For the 3rd highest, OFFSET 2. For the 5th highest, OFFSET 4.
Method 3: Correlated Subquery (Classic Interview Answer)
The traditional approach without window functions. Works in all databases but is slower.
-- Find salary where exactly N-1 salaries are higher:
SELECT DISTINCT salary
FROM employees e1
WHERE 3 - 1 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary > e1.salary
);
-- Explanation:
-- For the 3rd highest: count salaries above it = 2 (3-1)
-- For the 2nd highest: count salaries above it = 1 (2-1)
Tip: This method is O(n²) — slow on large tables. Use DENSE_RANK in real applications. Know this method for interviews.
Find Nth Highest Per Department
A common extension: find the highest salary within each group.
-- Top 3 salaries per department:
SELECT * FROM (
SELECT
name, department, salary,
DENSE_RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS dept_rank
FROM employees
) ranked
WHERE dept_rank <= 3
ORDER BY department, dept_rank;
Tip: PARTITION BY department creates separate rankings for each department. This is a powerful pattern for "top N per group" queries.
Handle Edge Cases
What if there is no Nth highest salary? Or if multiple employees share the same salary?
-- Return NULL if Nth doesn't exist:
SELECT COALESCE(
(SELECT DISTINCT salary
FROM (
SELECT salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
) ranked
WHERE rank = 5),
NULL
) AS fifth_highest_salary;
-- Get all employees at the Nth salary level:
WITH salary_ranks AS (
SELECT *, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
)
SELECT name, salary FROM salary_ranks WHERE rank = 3;
Tip: In interviews, always mention edge cases: empty table, fewer than N distinct salaries, and ties.
Best Practices
- Use DENSE_RANK for correctness with ties
- LIMIT OFFSET is simplest for MySQL/PostgreSQL
- Know the correlated subquery method for interviews
- Always handle edge cases (NULL when Nth doesn't exist)
- For "top N per group", combine DENSE_RANK with PARTITION BY
Generate SQL Queries with AI2SQL
Skip the syntax lookup. Describe what you need in plain English and AI2SQL generates the correct SQL query instantly.
No credit card required
Frequently Asked Questions
What is the best way to find the Nth highest salary?
Use DENSE_RANK() OVER (ORDER BY salary DESC) and filter WHERE rank = N. This handles ties correctly and works in all modern databases.
What is the difference between RANK, DENSE_RANK, and ROW_NUMBER?
ROW_NUMBER gives unique numbers (1,2,3,4). RANK gives same rank to ties but skips (1,2,2,4). DENSE_RANK gives same rank to ties without skipping (1,2,2,3).
Can AI2SQL solve SQL interview questions?
Yes. Describe the problem in plain English like "find the 3rd highest salary per department" and AI2SQL generates the correct query.