Before continuing: This entry is part of the window functions guide. If you are new to the topic, start with OVER.
Examples
WITH scores(name, points) AS (
VALUES ('Amy', 10), ('Bruno', 20), ('Celia', 20), ('David', 40)
)
SELECT
name,
points,
ROUND(PERCENT_RANK() OVER (ORDER BY points), 3) AS percent_rank,
ROUND(CUME_DIST() OVER (ORDER BY points), 3) AS cume_dist
FROM scores
ORDER BY points, name;| name | points | percent_rank | cume_dist |
|---|---|---|---|
| Amy | 10 | 0.0 | 0.25 |
| Bruno | 20 | 0.333 | 0.75 |
| Celia | 20 | 0.333 | 0.75 |
| David | 40 | 1.0 | 1.0 |
Bruno and Celia are peers because they have the same ordering value. They share RANK(), so they also share PERCENT_RANK(). In CUME_DIST(), the group of 20 reaches the third of four rows: 3/4 = 0.75.
Differences and trade-offs
Conceptual difference
| Function | Question | With ties |
|---|---|---|
PERCENT_RANK() | What relative rank does this peer group hold? | Uses the group's starting rank. |
CUME_DIST() | What share of rows sits at or below this one? | Uses the end of the peer group. |
NTILE(4) | Which group of four does this row fall into? | Aims for evenly sized groups and may split ties. |
PARTITION BY restarts the calculation for each group
If you compare employees within each department or products within each category, add PARTITION BY. Each partition gets its own row count and its own relative values.
WITH products(category, name, price) AS (
VALUES
('A', 'A1', 10), ('A', 'A2', 20), ('A', 'A3', 30),
('B', 'B1', 5), ('B', 'B2', 5), ('B', 'B3', 25)
)
SELECT
category,
name,
price,
PERCENT_RANK() OVER (
PARTITION BY category
ORDER BY price
) AS relative_position,
CUME_DIST() OVER (
PARTITION BY category
ORDER BY price
) AS cumulative_distribution
FROM products
ORDER BY category, price, name;They are neither the value's percentile nor the median
These functions describe the position of an observed row. They do not calculate the value at the 50th, 90th or 95th percentile. To get a percentile value, some engines offer functions such as PERCENTILE_CONT or PERCENTILE_DISC, with its own syntax and availability.
Mental model: PERCENT_RANK starts from the rank; CUME_DIST starts from how many rows are already covered by the current value.