Problems / Median Score by Department / Editorial
DuckDB provides MEDIAN(score) as a built-in aggregate — equivalent to PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY score).
MEDIAN(score)
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY score)
When MEDIAN isn't available, compute it from first principles:
ROW_NUMBER() OVER (PARTITION BY department ORDER BY score)
COUNT(*) OVER (PARTITION BY department)
FLOOR((n+1)/2)
CEIL((n+1)/2)
AVG(score)
For scores [68, 75, 82, 90] (n=4):
Solve Median Score by Department yourself →