Coding question · Top N Per GroupMediumAsked at Uber

Top-paid employee in each department

Top-N per group with window functions.

Back to all coding questions

The Brief

Comp teamVP Compensation#coding-interview

Return the highest-paid employee in each department. Columns: `department_name`, `name`, `salary`. Order by department name. Tables: `ci_employees`, `ci_departments`.

Tables & columns available

ci_employeesdim6 columns
ColumnTypeKey
idINTPK
nameTEXT
department_idINTFK → ci_departments.id
manager_idINTFK → ci_employees.id
salaryINT
hire_dateDATE
ci_departmentsdim2 columns
ColumnTypeKey
idINTPK
nameTEXT

Hints (2)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC).

Hint 2

Wrap in a CTE and filter where rn = 1.