Fifth Highest Salary In A Department
Problem statement
The table employees stores one row per employee, including that employee's department and salary.
For each department, rank that department's distinct salary values from highest to lowest using dense ranks. Equal salaries share one rank. Return the salary whose dense rank is exactly 5.
Omit any department that has fewer than five distinct salary values. Return department_id and fifth_highest_salary. Order rows by department_id ascending.
What the interview report shared
The interviewer asked SQL for the fifth-highest salary within a department, then asked how to order the result deterministically when salaries tie.
| Column | Meaning |
|---|---|
employee_id | Unique employee identifier |
employee_name | Employee display name |
department_id | Department identifier |
salary | Employee salary |
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
employees
One row per employee.
| Column | Type | Nullable | Description |
|---|---|---|---|
| employee_idPK | Integer | No | — |
| employee_name | Text | No | — |
| department_id | Integer | No | — |
| salary | Integer | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| department_id | Integer | No | — |
| fifth_highest_salary | Integer | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- The
employeestable has between1and10^5rows. employee_idis unique and non-null.employee_name,department_id, andsalaryare non-null.0 <= salary <= 10^9.