Comparison · Advanced SQL

PERCENT_RANK and CUME_DIST in SQL: relative position

PERCENT_RANK expresses relative position based on RANK; CUME_DIST measures the share of rows that come before or are tied. Ties produce different results.

Before continuing: This entry is part of the window functions guide. If you are new to the topic, start with OVER.

Examples

Comparing the two functions
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;
Result verified in SQLite 3.46.1
namepointspercent_rankcume_dist
Amy100.00.25
Bruno200.3330.75
Celia200.3330.75
David401.01.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

FunctionQuestionWith 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.

Position within each category
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.