Function Reference

PERCENT_RANK

Returns the relative rank of a row in a window partition: the share of rows before it, from 0 to 1.

PostgreSQL 17.5
PERCENT_RANK() OVER ([PARTITION BY ...] [ORDER BY ...])

Parameters

PARTITION BYoptional
Columns that split rows into partitions, ranks are computed in each one separately
ORDER BYoptional
Order of rows that defines the rank

Return value

A floating-point number (double precision) from 0 to 1

Examples

Relative rank by birth date

PostgreSQL 17.5
SELECT member_name,
	birthday,
	PERCENT_RANK() OVER (
		ORDER BY birthday
	) AS pct_rank
FROM FamilyMembers;
member_namebirthdaypct_rank
Headley Quincey1960-05-130
Ernest Forrest1961-09-110.14285714285714285
Flavia Quincey1963-02-160.2857142857142857
Constance Forrest1968-09-060.42857142857142855
Andie Quincey1983-06-050.5714285714285714
Lela Quincey1985-06-070.7142857142857143
Annie Quincey1988-04-100.8571428571428571
Wednesday Addams2005-01-131

RANK and PERCENT_RANK for prices

PostgreSQL 17.5
SELECT unit_price,
	RANK() OVER (
		ORDER BY unit_price
	) AS price_rank,
	ROUND(
		PERCENT_RANK() OVER (
			ORDER BY unit_price
		)::numeric,
		2
	) AS pct_rank
FROM Payments
ORDER BY unit_price;
unit_priceprice_rankpct_rank
710.00
710.00
830.07
830.07
830.07
1060.19
1670.22
2080.26
5990.30
100100.33

Details

It is computed as (RANK − 1) / (number of rows in the partition − 1). Equal values get the same rank. For a partition of one row the result is 0.

See also