Back to Practice
#0061
Second Highest Salary
MediumSQL18 min15 XP
Problem
Write a SQL query that returns one column named second_highest_salary containing the second distinct highest salary from employees. If there is no second distinct salary, return NULL.
Why This Matters
This is a classic SQL ranking question because duplicates make naive ORDER BY LIMIT answers wrong.
Schema
employees(id INTEGER, name TEXT, salary INTEGER)
Examples
Example 1
Inputemployees salaries = 100, 200, 200, 300
Output200
Distinct salaries are 300, 200, and 100, so the second highest is 200.
Constraints
- Return exactly one row.
- Use distinct salary values.
- Name the output column second_highest_salary.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Duplicate top salaries
Input100, 200, 200, 300
Expected200
Duplicates should not count as separate ranks.
Hidden Test Categories
Only one distinct salaryall salaries distinctnegative or zero salary values