PostgreSQL 18 reference · Aggregate Functions
PostgreSQL PERCENTILE_CONT() Function
Return the value at a percentile, interpolating between rows: percentile_cont(0.5) is the median.
PostgreSQL 18 Since 9.4
Syntax
percentile_cont(fraction) WITHIN GROUP (ORDER BY expression)
Parameters
| Parameter | Description |
|---|---|
fraction |
From 0 to 1, or an array of fractions. |
ORDER BY |
The values to rank. |
Return value
double precision, or an array.
Examples
SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary FROM employees;
Output
median_salary
---------------
127500
(1 row)Run this on your computer — see “Get Started” for setup.
SELECT percentile_cont(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY price) AS quartiles FROM products;
Output
quartiles
-----------------------
{28.74,40.975,67.175}
(1 row)Run this on your computer — see “Get Started” for setup.
percentile_disc() returns an actual value from the data instead of interpolating.
Version history
| Version | Change |
|---|---|
| PostgreSQL 9.4 | Added |
Related
MODE()· Return the most frequent value in a group (the statistical mode). Ties go to the first value in sort order.AVG()· Return the average of the non-NULL values in a group. Integer input gives a numeric result with full precision.- SQL Aggregate Functions · tutorial
- SQL GROUP BY · tutorial