Problem · Database

Top-K Salaries Per Department

Mediuminfosys logoinfosysFULLTIMEONSITE INTERVIEW

Problem statement

The table employees contains employee salaries and department assignments. The table query_parameters contains exactly one row with the requested value k.

For each department, rank its distinct salary levels from highest to lowest using dense ranks. Return every employee whose salary rank is at most k, including all employees tied at a qualifying salary.

Return department_id, employee_id, employee_name, salary, and salary_rank. Order rows by department_id ascending, then salary descending, then employee_id ascending. Do not use LIMIT.

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

query_parameters

Exactly one row containing the requested number of distinct salary levels.

ColumnTypeNullableDescription
kIntegerNo

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
department_idIntegerNo
employee_idIntegerNo
employee_nameTextNo
salaryIntegerNo
salary_rankIntegerNo

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

Constraints

  • 1 <= employees.length <= 200000
  • employee_id is unique.
  • department_id, employee_name, and salary are non-NULL.
  • 0 <= salary <= 1000000000
  • query_parameters contains exactly one row and 1 <= k <= 100.

More infosys problems