PostgreSQL 18 reference · Window Functions
PostgreSQL PERCENT_RANK() Function
Return a row's relative rank from 0 to 1: (rank - 1) / (rows in partition - 1).
PostgreSQL 18 Since 8.4
Syntax
percent_rank() OVER (PARTITION BY ... ORDER BY ...)
Return value
double precision.
Examples
SELECT name, salary, round(percent_rank() OVER (ORDER BY salary)::numeric, 2) AS pct
FROM employees
WHERE department = 'Engineering';
Output
name | salary | pct --------------+--------+------ Lucas Martin | 112000 | 0.00 Priya Nair | 115000 | 0.25 Jonas Berg | 140000 | 0.50 Mei Chen | 165000 | 0.75 Omar Haddad | 210000 | 1.00 (5 rows)
Run this on your computer — see “Get Started” for setup.
SELECT name, price, round(percent_rank() OVER (ORDER BY price)::numeric, 2) AS pct
FROM products
WHERE category_id = 3;
Output
name | price | pct -------------------+-------+------ Coffee Mug | 12.00 | 0.00 Desk Lamp | 45.00 | 0.50 Standing Desk Mat | 59.90 | 1.00 (3 rows)
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 8.4 | Added |
In other languages
PERCENT_RANK()· SQLite
Related
CUME_DIST()· Return the share of rows ordered at or before the current row (including ties), from just above 0 to 1.NTILE()· Split the rows of a window into N groups of nearly equal size and return each row's group number.- SQL Window Functions · tutorial