Skip to content
Onsite

Nth highest salary

MediumP1Coding

Table employee:

ColumnType
idint (primary key)
salaryint

Write a query that returns the third highest distinct salary as one column named third_highest_salary. If there are fewer than three distinct salaries, return one row with NULL.

Schema

CREATE TABLE employee (
  id int PRIMARY KEY,
  salary int NOT NULL
);

Example 1

Input
employee: (1, 100), (2, 200), (3, 300), (4, 400)
Output
third_highest_salary: 200

Example 2

Input
employee: (1, 100), (2, 100), (3, 200)
Output
third_highest_salary: NULL
Why
Only two distinct salaries.

Constraints

  • PostgreSQL dialect.
  • Duplicates count once.
  • N is fixed at 3 here; the same query shape works for any N.

Hints

PostgreSQL
Run checks the 2 visible tests, Submit all 6.