Nth Highest Distinct Salary
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Description
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 Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| name | VARCHAR | Employee name |
| salary | INT | Annual salary |
Database Schema (Inferred)
Employees
| Column Name | Example Value |
|---|---|
| id | 1 |
| name | Alice |
| salary | 90000 |
Example
Employees
| id | name | salary |
|---|---|---|
| 1 | Alice | 90000 |
| 2 | Bob | 85000 |
| 3 | Carol | 85000 |
| 4 | Dave | 80000 |
| 5 | Eve | 75000 |
Output
| third_highest_salary |
|---|
| 80000 |
Explanation:
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.
SQL Editor
Loading...
Employees
| id | name | salary |
|---|---|---|
| 1 | Alice | 90000 |
| 2 | Bob | 85000 |
| 3 | Carol | 85000 |
| 4 | Dave | 80000 |
| 5 | Eve | 75000 |
Output
| third_highest_salary |
|---|
| 80000 |