FastPrepFifth Highest Salary In A Department

Fifth Highest Salary In A Department

ZS logoZS● MediumNEW GRADONSITE INTERVIEW

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.

employees
ColumnMeaning
employee_idUnique employee identifier
employee_nameEmployee display name
department_idDepartment identifier
salaryEmployee salary

Table schema

MySQLPostgreSQLPandas

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.

ColumnTypeNullableDescription
employee_idPKIntegerNo—
employee_nameTextNo—
department_idIntegerNo—
salaryIntegerNo—

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
department_idIntegerNo—
fifth_highest_salaryIntegerNo—

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • The employees table has between 1 and 10^5 rows.
  • employee_id is unique and non-null.
  • employee_name, department_id, and salary are non-null.
  • 0 <= salary <= 10^9.

More ZS problems

See ZS hiring insights