Median salary by department
The task
The people team reports pay with the median, because a few very high salaries pull the average up.
For each department, use the current employees only (no termination_date). Employees with no department are left out.
Return department_name, headcount (current employees in the department) and median_salary, the median of their salary, rounded to 2 decimal places. When the headcount is even, the median is the average of the two middle salaries. Departments with no current employees are not listed.
Sort by department_name.
Expected output
department_nameheadcountmedian_salary- Sorted by department_name ascending.
- Numbers rounded to 2 decimal places.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables People team · click a name to insert it
departments7 rows in the sample
One row per department. Key: department_id.
| int | Unique id. | |
| text | Name. | |
| text | Main office city. |
employees120 rows in the sample
Everyone who has worked here, including people who left. Key: employee_id.
| int | Unique id. | |
| text | Name. | |
| int | Department. NULL for a few people between teams. | |
| int | Their manager (an employee_id). NULL for the CEO. | |
| text | Title. | |
| date | First day. | |
| date | Last day. NULL while employed. | |
| numeric(10,2) | Current (or final) yearly salary, USD. |
salary_changes367 rows in the sample
Every salary someone has had, from the day it applied. Key: employee_id, effective_on.
| int | The employee. | |
| date | First day of this salary. | |
| numeric(10,2) | Yearly salary, USD. |
reviews301 rows in the sample
Year-end review ratings. Key: employee_id, review_year.
| int | The employee. | |
| int | Year reviewed. | |
| int | 1 (low) to 5 (high). |
Related questions
- Median listen length, continuous and discreteeasy
- 90th percentile trip length by citymedium
- Median dining spend by month, without percentile functionshard
- Purchase value spread (interquartile range) by platformmedium
- Employees paid above their department averageeasy
- Skip-level managers and how many people they covereasy
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL