Back to Practice
#0158

Second Highest Salary

MediumSQL25 min20 XP

Problem

Return the second highest distinct salary from employees. If it does not exist, return no rows.

Why This Matters

Ranking with ties is a common interview SQL pattern and a practical analytics requirement.

Schema

employees(id INTEGER, salary INTEGER)

Examples

Example 1
Inputsalaries: 100, 200, 200, 300
Output200

Distinct salaries are 300, 200, 100, so the second highest is 200.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Keep the code readable and deterministic.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputsalaries: 100, 200, 200, 300
Expected200

Distinct salaries are 300, 200, 100, so the second highest is 200.

Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input