← Back to SQL, page 13

sql-0250 · Easy fresher

Write a query that returns the second-highest salary, and returns NULL if there is no second value.

Take the highest salary below the maximum: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). Because MAX over zero rows returns NULL, the query returns one row with NULL when everyone earns the same. Ties at the top are handled, since the comparison is on distinct values.

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
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
second_highest
95000

The top salary is 120000; the highest below it is 95000, even though two people earn it.

If every salary were the same, the inner condition would match no rows and MAX would return NULL rather than an empty result:

sql
WITH same(salary) AS (VALUES (50000), (50000))
SELECT MAX(salary) AS second_highest FROM same WHERE salary < (SELECT MAX(salary) FROM same);
second_highest
NULL

The LIMIT 1 OFFSET 1 version over SELECT DISTINCT salary ... ORDER BY salary DESC also works, but it returns no row at all when there is no second value, which some callers handle badly. Wrapping it as a scalar subquery restores the NULL:

sql
SELECT (SELECT DISTINCT salary FROM employees WHERE salary IS NOT NULL
        ORDER BY salary DESC LIMIT 1 OFFSET 1) AS second_highest;
second_highest
95000
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');

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

WITH same(salary) AS (VALUES (50000), (50000))
SELECT MAX(salary) AS second_highest FROM same WHERE salary < (SELECT MAX(salary) FROM same);

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

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • MAX below MAX gives the second-highest distinct value
  • MAX over no rows returns NULL
  • DISTINCT with OFFSET 1 is an alternative
  • Wrap in a scalar subquery to return NULL instead of no rows

Common mistakes

  • Using OFFSET 1 without DISTINCT and returning a tie
  • Returning an empty result when NULL was expected

Follow-ups

  • How would you generalize to the Nth highest?
  • How are NULL salaries treated?

Tips

  • Mention ties and the empty case