← Back to SQL, page 7

sql-0123 · Medium mid

What do PERCENT_RANK and CUME_DIST return, and how do they differ?

PERCENT_RANK is (rank - 1) / (rows - 1): the share of other rows that rank strictly below, from 0 for the first row to 1 for the last. CUME_DIST is (rows with a value at or before the current one) / (total rows): the share of rows at or below the current value, from above 0 up to 1. Ties share the same value in both.

Full answer
sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name, salary,
       ROUND(PERCENT_RANK() OVER (ORDER BY salary), 3) AS pct_rank,
       ROUND(CUME_DIST()    OVER (ORDER BY salary), 3) AS cume_dist
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary, name;
namesalarypct_rankcume_dist
Omar480000.00.143
Diego550000.1670.286
Sara620000.3330.429
Tom700000.50.571
Mei950000.6670.857
Ravi950000.6670.857
Asha1200001.01.0

With seven salaries in ascending order:

  • Omar is lowest. PERCENT_RANK is 0, because no one ranks below him. CUME_DIST is 1/7, because one of seven rows is at or below his salary.
  • Ravi and Mei tie at 95000. Their RANK is 5, so PERCENT_RANK is (5 - 1) / (7 - 1) = 0.667. Six of seven salaries are at or below 95000, so CUME_DIST is 6/7 = 0.857 for both.
  • Asha is highest: PERCENT_RANK 1 and CUME_DIST 1.

Use CUME_DIST for statements like "this salary is at or above 85.7 percent of salaries". Use PERCENT_RANK when you want the first row at exactly 0, for scales from 0 to 100.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, salary,
       ROUND(PERCENT_RANK() OVER (ORDER BY salary), 3) AS pct_rank,
       ROUND(CUME_DIST()    OVER (ORDER BY salary), 3) AS cume_dist
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary, name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • PERCENT_RANK = (rank - 1) / (n - 1)
  • CUME_DIST = rows at or before current / n
  • Ties share the same value
  • PERCENT_RANK starts at 0; CUME_DIST never does

Common mistakes

  • Calling PERCENT_RANK a percentile of the value itself
  • Forgetting NULL handling; NULLs are ranked too unless filtered

Follow-ups

  • How would you find the median with these functions?
  • How would you report each student's percentile within their course?

Tips

  • Work one tied row through both formulas