Find Second Highest Employee Wage

Quick Overview

Write a SQL query that returns the second highest distinct wage from an employee table, or a single NULL row when no such wage exists. It tests handling of duplicate values, scalar subqueries, ranking, and the difference between an empty result and a NULL result.

Find Second Highest Employee Wage

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `staff_wages` holds one employee's id and wage. Find the second highest distinct wage in the table and return it as `second_highest_wage`. If there is no second highest distinct wage, return `NULL`. ```hint Empty case When no second wage exists, a query can return zero rows or one row holding NULL. Only the second one matches the contract, so check what your query does on a table with a single distinct wage. ``` ### Input Table(s) `staff_wages` | Column | Type | Meaning | |---|---|---| | staff_id | INT | Employee identifier | | wage | INT | The employee's wage | Primary key: `staff_id` (unique values). ### Output Contract | Column | Type | Meaning | |---|---|---| | second_highest_wage | INT | The second highest distinct wage, or NULL if there is none | Always return exactly one row with this one column. With a single row there is no ordering to specify. No rounding applies. ### Example `staff_wages` | staff_id | wage | |---|---| | 1 | 500 | | 2 | 700 | | 3 | 900 | Expected result: | second_highest_wage | |---| | 700 | The wages are 500, 700 and 900, so the highest is 900 and the second highest distinct wage is 700. If every employee earned 900, there would be only one distinct wage, and the expected result would be a single row with `second_highest_wage` equal to NULL. ### Constraints and Clarifications - Wages are compared as distinct values. With wages 900, 900 and 700 the answer is 700, because the two 900s count as one value. - If the table is empty or has only one distinct wage, return one row containing NULL. An empty result set is wrong. - Ignore rows whose `wage` is NULL; they do not count as a wage. - Several employees may share the second highest wage. The answer is still the single wage value.

Overview: Write a SQL query that returns the second highest distinct wage from an employee table, or a single NULL row when no such wage exists. It tests handling of duplicate values, scalar subqueries, ranking, and the difference between an empty result and a NULL result.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of staff_wages holds one employee's id and wage. Find the second highest distinct wage in the table and return it as second_highest_wage. If there is no second highest distinct wage, return NULL.

Input Table(s)

staff_wages

ColumnTypeMeaning
staff_idINTEmployee identifier
wageINTThe employee's wage

Primary key: staff_id (unique values).

Output Contract

ColumnTypeMeaning
second_highest_wageINTThe second highest distinct wage, or NULL if there is none

Always return exactly one row with this one column. With a single row there is no ordering to specify. No rounding applies.

Example

staff_wages

staff_idwage
1500
2700
3900

Expected result:

second_highest_wage
700

The wages are 500, 700 and 900, so the highest is 900 and the second highest distinct wage is 700.

If every employee earned 900, there would be only one distinct wage, and the expected result would be a single row with second_highest_wage equal to NULL.

Constraints and Clarifications

  • Wages are compared as distinct values. With wages 900, 900 and 700 the answer is 700, because the two 900s count as one value.
  • If the table is empty or has only one distinct wage, return one row containing NULL. An empty result set is wrong.
  • Ignore rows whose wage is NULL; they do not count as a wage.
  • Several employees may share the second highest wage. The answer is still the single wage value.