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;| name | salary | pct_rank | cume_dist |
|---|---|---|---|
| Omar | 48000 | 0.0 | 0.143 |
| Diego | 55000 | 0.167 | 0.286 |
| Sara | 62000 | 0.333 | 0.429 |
| Tom | 70000 | 0.5 | 0.571 |
| Mei | 95000 | 0.667 | 0.857 |
| Ravi | 95000 | 0.667 | 0.857 |
| Asha | 120000 | 1.0 | 1.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