Example 1
Show relative rank for each test score
Dan is first in ascending score order, so his percent rank is 0. Bob and Carol tie at 80 and share the same rank values. Ada is last and reaches the top of the cumulative distribution.
CREATE TABLE scores (student VARCHAR(20), score INT);
INSERT INTO
scores
VALUES
('Ada', 95),
('Bob', 80),
('Carol', 80),
('Dan', 60);SELECT
student,
score,
PERCENT_RANK() OVER (
ORDER BY
score
) AS percent_rank,
CUME_DIST() OVER (
ORDER BY
score
) AS cume_dist
FROM
scores
ORDER BY
score,
student;Floating-point formatting can vary slightly by engine, so expected rows are not fixed here.