← All Exercises
Median Salary per Department with PERCENTILE_CONT
intermediatesql
Background
Averages hide skewed pay distributions. Recruiters compare median and mean pay to see whether a few very high earners pull the average upward.
Problem
Given an employees table, return each department's median salary and average salary, both rounded to two decimals and ordered by department name.
Input
A PostgreSQL employees table with id integer PRIMARY KEY, department text NOT NULL, and salary numeric(10,2) NOT NULL.
Output
One row per department with department, median_salary, and average_salary as numeric values rounded to two decimals, ordered by department.
Sample tests
Sample 1
Input: Engineering salaries 80000, 100000, 120000, 140000; Sales salaries 50000, 60000, 90000
Expected: Engineering | 110000.00 | 110000.00 Sales | 60000.00 | 66666.67
Sample 2
Input: a separate dataset containing only Support with a single salary of 72000
Expected: Support | 72000.00 | 72000.00
Your solution
Learning objective
Use PostgreSQL ordered-set aggregates, cast double precision to numeric, and reason about median versus mean.