AggregateSQL standard

PERCENTILE_DISC

Finds a percentile among existing values — a median that's definitely in the data.

Syntax

PERCENTILE_DISC(<fraction>) WITHIN GROUP (ORDER BY <expression>)

Sorts the values and returns the first one at which the given share of rows is reached. Unlike PERCENTILE_CONT, it doesn't invent a midpoint between two values, so it works with dates and text too.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

The median salary
SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary
FROM employees;
median_salary
1200
CONT versus DISC in each department
SELECT department,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS cont,
PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary) AS disc
FROM employees
GROUP BY department
ORDER BY department;
departmentcontdisc
backend11001000
design12001200
frontend1150950

backend has two salaries: CONT takes the midpoint between them — 1100, and DISC takes the smaller existing one, 1000.

The median order date
SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY created_at) AS median_order
FROM orders;
median_order
2026-08-09 09:05:00

PERCENTILE_CONT doesn't work with dates, while DISC simply takes one of the existing ones.

Watch out

With an even number of values, it takes the smaller of the two middle ones: for 1000 and 1200 — 1000, not 1100.

The syntax is the same as in PERCENTILE_CONT: a fraction from 0 to 1 in the brackets, and the column in WITHIN GROUP (ORDER BY …).

It can't be a window function with OVER: OVER is not supported for ordered-set aggregate percentile_disc.

Related functions