Interview Questions/SQL/Nth Highest Distinct Salary

Nth Highest Distinct Salary

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Nth Highest Distinct Salary

You are given an Employees table with salaries. Write a SQL query to find the 3rd highest distinct salary. If fewer than 3 distinct salary values exist, return an empty result. Return a single column named third_highest_salary. Table: Employees

Column NameTypeDescription
idINTPrimary key
nameVARCHAREmployee name
salaryINTAnnual salary

Examples

Example 1

Distinct salaries in descending order: 90000, 85000, 80000, 75000. The 3rd distinct value is 80000. Note that 85000 appears twice but counts once.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

query.sql