Skip to main content

Second-Highest Salary in SQL: Answers and Edge Cases

Compare four SQLite-ready approaches, clarify what “second” means, and handle ties, NULLs and missing salary levels.

Updated 6 min readMarkdown
Second-Highest Salary in SQL: Answers and Edge Cases

Second-highest salary: define the question first

“Second-highest salary” can mean the second row after sorting or the second distinct salary. If two employees tie for the highest salary, those interpretations give different answers. State your assumption or ask which meaning the interviewer intends.

We use the second distinct, non-NULL salary. That interpretation treats ties as one salary level. The examples below use SQLite and this table:

CREATE TABLE Employee (
  id INT,
  name TEXT,
  salary INT
);

INSERT INTO Employee (id, name, salary) VALUES
  (1, 'Asha', 120),
  (2, 'Bilal', 120),
  (3, 'Chen', 90),
  (4, 'Devi', 70),
  (5, 'Eshan', NULL);

There are two employees tied at 120, then salaries of 90 and 70. The expected second distinct salary is 90.

1. MAX with a subquery

This is compact and handles a tie at the top naturally:

SELECT MAX(salary)
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);

The inner query finds 120. The outer query considers salaries below 120 and returns their maximum, 90. MAX ignores NULL values, so Eshan’s row does not affect the result. If the table has no salary below its maximum, the outer MAX returns NULL. For example, if every non-NULL salary is 120, there is no second distinct value to return.

This is often the clearest answer when the interviewer means a distinct salary level. Say that it returns NULL when that level does not exist; naming that behaviour is part of a complete answer.

2. DISTINCT, ORDER BY and OFFSET

Another readable option selects distinct salary values in descending order and skips the first:

SELECT DISTINCT salary
FROM Employee
WHERE salary IS NOT NULL
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

SQLite returns one row containing 90. Without a second distinct salary, this query returns no row. That is different from a single NULL value. If a scalar result is required, wrap the selection in a scalar subquery:

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

This returns one row with 90, or one row with NULL when no second value exists. The explicit NULL filter makes the intent clear and avoids relying on each database’s default NULL sort position.

The row-limiting syntax varies by database. SQL Server uses ORDER BY with OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY, or a TOP form. Oracle 12c and later supports OFFSET … FETCH. Use the dialect the interviewer specifies.

3. DENSE_RANK for tied rows

A window function makes the rank of each distinct salary level visible:

SELECT salary
FROM (
  SELECT salary,
         DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank
  FROM Employee
  WHERE salary IS NOT NULL
)
WHERE salary_rank = 2;

With the sample data, both employees on the top salary receive rank 1, and 90 receives rank 2. The result is one row containing 90. If you want employee details, select id, name and salary in the inner query and return the columns you need outside it; tied employees at the second salary would then both appear.

The choice of ranking function matters. ROW_NUMBER() gives every row a unique number, so a tie at 120 may receive row numbers 1 and 2 in either order. Filtering for row number 2 could return 120 again, rather than the next salary level. RANK() assigns both top rows rank 1, then skips rank 2, so filtering for rank 2 returns no rows in this sample. DENSE_RANK() assigns the next distinct value rank 2, which matches the stated interpretation.

4. A correlated subquery for the Nth value

For a general Nth-highest distinct salary, count how many distinct salaries are greater than each candidate salary. For the second-highest, substitute 2 - 1 for N - 1:

SELECT DISTINCT e1.salary
FROM Employee AS e1
WHERE e1.salary IS NOT NULL
  AND 1 = (
    SELECT COUNT(DISTINCT e2.salary)
    FROM Employee AS e2
    WHERE e2.salary > e1.salary
  );

For salary 90, exactly one distinct salary is greater, so it qualifies. A top salary has zero greater values; 70 has two. The NULL filter keeps NULL from becoming a candidate. This is flexible, but without help from an index the repeated comparisons can take O(n²) work as the table grows. For a fixed second-place question, MAX or DENSE_RANK is usually easier to explain.

Edge cases interviewers check

A tie at the top

Decide whether the question asks for the next distinct value or the second employee row. MAX with a subquery, DISTINCT, and DENSE_RANK answer the distinct-value version. A row-number solution answers a row-based question only when its tie ordering is specified.

NULL salaries

MAX and COUNT ignore NULL values. The LIMIT approach should filter them explicitly. Default descending NULL order differs: PostgreSQL and Oracle put NULLs first, while MySQL and SQLite put them last. An explicit WHERE salary IS NOT NULL avoids that variation.

Fewer than two distinct salaries

The MAX query returns NULL; the direct LIMIT query returns no row; its scalar-subquery form returns NULL. Explain which shape your application expects.

Per-department results

Partition the ranking by department, then select rank 2 within each group:

SELECT dept_id, id, name, salary
FROM (
  SELECT dept_id, id, name, salary,
         DENSE_RANK() OVER (
           PARTITION BY dept_id
           ORDER BY salary DESC
         ) AS salary_rank
  FROM Employee
  WHERE salary IS NOT NULL
)
WHERE salary_rank = 2;

This version assumes Employee also has a dept_id column. It returns each employee tied at their department’s second distinct salary.

A concise interview explanation

Start with: “I’ll treat second-highest as the second distinct salary, so ties at the top share first place. Is that the intended meaning?” Then choose the query that fits the requested output. Mention what happens if there is no second value and how NULL salaries are handled. Those details show that you understand the result as well as the syntax.

For more practice, our SQL interview questions page covers Nth-highest salary, ROW_NUMBER versus RANK versus DENSE_RANK, and why NOT IN can be dangerous with NULLs.

About the author

  • NostrobeAI provides resume-aware answers during live coding, system design and behavioural interviews. Practise with role-specific AI mock interviews, review feedback and explore a free library of 2,000+ interview questions with answers.

No comments yet. Be the first to comment.