QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Interview tasks

Task: find the second highest salary.

The second highest salary is usually the second largest distinct value, so the condition is clarified first. The simplest way is the maximum among values below the overall maximum. The LIMIT 1 OFFSET 1 variant counts rows, not values, so it needs DISTINCT and NULL filtering (in PostgreSQL NULL comes first under DESC). For the Nth highest, DENSE_RANK fits.

SQL · Tasks

Two people share the top salary. Which one is then the "second highest"?

In an interview you get an employees table and are asked for the second highest salary. The candidate writes ORDER BY salary DESC LIMIT 1 OFFSET 1, the query runs and returns a number. The number is wrong. This lesson shows how to take such a task apart: clarify the condition, pick an approach and check it on data with repeats and NULL.

namesalary
Anna300
Bohdan300
Olha250
Ivan200
KaterynaNULL

What to clarify first. "Second highest" reads in two ways: the second value (250 is expected) or the second row of the list (300, because Bohdan also earns the maximum). Usually the unique value is meant, but say it out loud. Two more questions: what to return when there is no second one (a row with NULL or an empty result) and whether the column may hold NULL.

How it works and why. The three approaches answer the question differently. The subquery looks for "the highest among those below the maximum": repeats of the maximum do not interfere, because < drops every copy, and NULL gives UNKNOWN and falls out of the selection by itself. An aggregate without GROUP BY always returns one row, so when no row qualifies MAX gives NULL. OFFSET works with rows, not values: it sorts and skips N rows, so repeats must be removed with DISTINCT before the skip. DENSE_RANK numbers values without gaps, equal salaries get one rank, so "the N-th" becomes dr = N.

There is also a trap that depends on the DBMS. In PostgreSQL, with DESC NULL values come first by default (NULLS FIRST); in MySQL NULL counts as the smallest and ends up last. The same query can therefore give different answers on different databases.

Example: from a wrong answer to the fix. The initial query:

SELECT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
-- 300

Diagnosis: in PostgreSQL the sorted list looks like NULL, 300, 300, 250, 200. OFFSET 1 skips NULL and LIMIT 1 takes the next row, i.e. 300. There are two defects: NULL comes first and the repeated maximum takes second place.

How OFFSET 1 skips and cuts rows

No DISTINCT, NULL first
№salary
1NULL OFFSET
2300
3300 LIMIT
4250 LIMIT
5200 LIMIT
With DISTINCT and IS NOT NULL
№salary
1300 OFFSET
2250
3200 LIMIT
Figure 1. The surviving row is the answer. On the left it is 300: the second row of the list, not the second value. On the right it is 250.

Fixed variants (on this data all return 250):

-- 1. subquery
SELECT MAX(salary) AS second_max
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- 250

-- 2. DISTINCT + OFFSET
SELECT DISTINCT salary
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
-- 250

-- 3. DENSE_RANK: change dr = N for the N-th
WITH r AS (
  SELECT salary,
         DENSE_RANK() OVER (ORDER BY salary DESC) AS dr
  FROM employees
  WHERE salary IS NOT NULL
)
SELECT DISTINCT salary FROM r WHERE dr = 2;
-- 250

Why these fixes. DISTINCT turns a list of rows into a list of values, IS NOT NULL removes NULL before sorting (the alternative is ORDER BY salary DESC NULLS LAST). Without either fix the answer stays 300.

Limits and alternatives. When there is no second salary (all equal, or just one), the subquery returns one row with NULL, while variants 2 and 3 return an empty result. A test that expects NULL fails on an empty set. To always get one row, wrap the query in a scalar subquery: with no rows it yields NULL.

-- always one row, even when there is no second salary
SELECT (
  SELECT DISTINCT salary
  FROM employees
  WHERE salary IS NOT NULL
  ORDER BY salary DESC
  LIMIT 1 OFFSET 1
) AS second_max;

The DENSE_RANK variant is costlier because it ranks the whole table, but it generalises best: the third highest is dr = 3, and it works per group with PARTITION BY. For the highest or the second highest a subquery is enough. SQL Server uses OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY instead of LIMIT.

Common mistakes.

  • OFFSET 1 without DISTINCT: the second row instead of the second value when the maximum repeats.
  • Ignoring NULL: in PostgreSQL it takes first place under DESC and shifts everything.
  • MAX(salary) - 1 and similar tricks: they only work on lucky data.
  • Not agreeing on the result when there is no second salary: the test expects NULL, the query returns zero rows.

Check yourself

The table has three rows with salary = 100. What do variants 1, 2 and 3 return?

Answer

Variant 1 returns one row with NULL (an aggregate without GROUP BY always yields a row, and salary < 100 selected nobody). Variants 2 and 3 return an empty result: after DISTINCT one value is left, nothing to skip and take. Agree what counts as the right answer.

In PostgreSQL SELECT salary FROM employees ORDER BY salary DESC LIMIT 1 returned NULL on our table. Why is it not the maximum?

Answer

For DESC PostgreSQL puts NULL first by default. The first row of the sorted list is NULL, not 300. The fix: WHERE salary IS NOT NULL or NULLS LAST. The MAX aggregate ignores NULL, so it does not have this problem.

In an interview. Junior: First I ask whether a repeat of the maximum counts. The simplest solution is the maximum salary among those below the overall maximum. The LIMIT and OFFSET variant needs DISTINCT, otherwise a repeated maximum gives a wrong answer. Middle/Senior: I also ask about NULL and what to return when there is no second salary: in PostgreSQL NULL comes first under DESC, and the subquery and OFFSET behave differently on an empty set. For the N-th highest I use DENSE_RANK, since it generalises and works inside groups.

More articles in this chapter