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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

percent_rank() OVER (PARTITION BY ... ORDER BY ...)

Return value

double precision.

Examples

Relative position
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.

Within a category
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

VersionChange
PostgreSQL 8.4Added

In other languages

Was this page helpful?