Percentile Discrete Median Per Group

Compute the exact median per group using the percentile_disc window function with a 0.5 fraction parameter.

SqlData Integrity

Code

snippet
SELECT DISTINCT
  department,
  PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary)
    OVER (PARTITION BY department) AS median_salary
FROM employees
ORDER BY department;