← Back to SQL, page 13

sql-0251 · Medium mid

Write a query that returns the employees earning the Nth highest distinct salary, for N = 3.

Rank salaries with DENSE_RANK() OVER (ORDER BY salary DESC) and keep rows where the rank equals N. DENSE_RANK gives tied salaries the same rank without gaps, so rank 3 means the third distinct salary level, and all employees at that level are returned.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
WITH r AS (
  SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees WHERE salary IS NOT NULL
)
SELECT name, salary FROM r WHERE rnk = 3 ORDER BY name;
namesalary
Tom70000

The distinct levels are 120000, 95000 and 70000, so the third is 70000 and Tom is returned. With RANK instead, the tie at 95000 would make the next rank 4, and rnk = 3 would match nobody.

A correlated version without window functions counts the distinct salaries above each row:

sql
SELECT name, salary FROM employees e
WHERE salary IS NOT NULL
  AND (SELECT COUNT(DISTINCT salary) FROM employees x WHERE x.salary > e.salary) = 2
ORDER BY name;
namesalary
Tom70000

The window form is clearer and scans once. NULL salaries are excluded so they do not take a rank.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

WITH r AS (
  SELECT name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees WHERE salary IS NOT NULL
)
SELECT name, salary FROM r WHERE rnk = 3 ORDER BY name;

SELECT name, salary FROM employees e
WHERE salary IS NOT NULL
  AND (SELECT COUNT(DISTINCT salary) FROM employees x WHERE x.salary > e.salary) = 2
ORDER BY name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • DENSE_RANK gives distinct salary levels without gaps
  • Filter rank = N in an outer query
  • RANK would skip numbers after ties
  • Exclude NULLs before ranking

Common mistakes

  • Using ROW_NUMBER and picking one employee arbitrarily
  • Using RANK and missing the level after a tie

Follow-ups

  • How would you return N per department?
  • What if N is larger than the number of levels?

Tips

  • Say why DENSE_RANK and not RANK