Coding question · Top N Per GroupVery HardAsked at Airbnb

Median salary per department

PERCENTILE_CONT for median.

Back to all coding questions

The Brief

Comp teamVP Compensation#coding-interview

For each department that has employees, return the median salary. Columns: `department_name`, `median_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 (3)

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

Hint 1

PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) is the Postgres way.

Hint 2

Group by department and join to ci_departments for the name.

Hint 3

PERCENTILE_CONT interpolates between values for even-sized groups; PERCENTILE_DISC picks the nearest existing value.