MediumDatabaseUpdated Sep 2026

Second Highest Salary

Asked at Adobe

Difficulty

Medium

Time

O(n)

Space

O(1)

Pattern

Database

Companies

1

Problem

You are given a table called Employee with two columns: id (an integer primary key) and salary (an integer). Each row describes one employee and their salary.

Write a SQL query that returns the second highest distinct salary in the table. "Distinct" means repeated salary values count only once — if two employees both earn 300, that is a single salary value, not two.

The result must be a single column named SecondHighestSalary with one row. If there is no second highest distinct salary (for example, the table is empty or every employee has the same salary), return null instead of an empty result.

Pick the second distinct salary

distinct salaries (high to low)300200100012Position 1 is 300, so the answer is the value atposition 2, which is 200.distinct salaries (high to low)1000Only one distinct salary exists, so position 2 ismissing and the query returns null.
Sort the distinct salaries from high to low and take the value in position 2.

Constraints

  • ●id is the primary key of the Employee table, so every id is unique
  • ●1 <= number of rows <= 10^5
  • ●0 <= salary <= 10^5
  • ●The answer column must be named SecondHighestSalary
  • ●Return exactly one row; use null when there is no second highest distinct salary

Examples

Example 1
InputEmployee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
Output+---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+
WhyThe distinct salaries are 100, 200 and 300. Sorted from high to low they are 300, 200, 100, so the second one is 200.
Example 2
InputEmployee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | +----+--------+
Output+---------------------+ | SecondHighestSalary | +---------------------+ | null | +---------------------+
WhyThere is only one distinct salary, so no second highest value exists. The query must still return one row, and that row holds null.

Asked At

1 companies
CompanyDifficulty
AdobeMediumView all Adobe questions →

How to Think About It

1

Sort all salaries in descending order and pick the second distinct value.

2

Use a subquery to find the maximum salary, then find the maximum salary less than that.

3

Use LIMIT and OFFSET to skip the first row after ordering.

4

Use the DISTINCT keyword to handle duplicate salary values.

5

Combine a subquery with IS NULL check to return null when only one salary exists.

Optimal Approach

Use a subquery that finds the maximum salary strictly less than the overall maximum salary. First compute MAX(salary) from the table, then compute MAX(salary) from the table where salary is less than that value. This naturally returns null if no second highest salary exists. An alternative approach uses ORDER BY salary DESC DISTINCT with LIMIT 1 OFFSET 1, which is concise but requires DISTINCT support.

Max below the overall max

salaries100200300012Step 1: the inner query MAX(salary) finds the highestsalary, 300.salaries < 30010020001Step 2: keep only salaries below 300, then take MAX ofthose, which is 200.SELECT MAX(salary) AS SecondHighestSalaryFROM EmployeeWHERE salary < (SELECT MAX(salary) FROM Employee)
Find the top salary first, then find the largest salary strictly smaller than it.
ComplexityTime O(n)Space O(1)The engine scans the table once to find the maximum salary and once more to find the largest salary below it, and it keeps only a running maximum in memory.

What Trips People Up in Real Interviews

1

Clarify whether duplicate salaries should be counted as one or two.

2

Handle the edge case where fewer than two distinct salaries exist by returning null.

3

Always use DISTINCT if the problem says "second highest" implies unique values.

4

Avoid using TOP or FETCH in databases that prefer LIMIT syntax unless specified.

5

Test your query mentally with a single-row table to confirm null behavior.

Solution Code

O(n) time
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Second Highest Salary problem?

Write a SQL query to find the second highest distinct salary from the Employee table. If there is no second highest salary, the query should return null.

How do you solve Second Highest Salary?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Second Highest Salary?

Second Highest Salary is asked at Adobe. It is a medium difficulty problem.

What are common mistakes on Second Highest Salary?
  • Clarify whether duplicate salaries should be counted as one or two.
  • Handle the edge case where fewer than two distinct salaries exist by returning null.
  • Always use DISTINCT if the problem says "second highest" implies unique values.
  • Avoid using TOP or FETCH in databases that prefer LIMIT syntax unless specified.
  • Test your query mentally with a single-row table to confirm null behavior.