Sub topic 14
Buckets and Percentiles: NTILE, PERCENT_RANK, and CUME_DIST
This chapter assumes you know SELECT, ORDER BY, aggregate functions, and the OVER clause. It also assumes you understand that a window function calculates a value for each row without removing the original rows.
Ranking functions answer questions such as “What position is this row in?” Distribution functions answer questions such as “How does this row compare with the rest of the set?”
Dividing rows into buckets with NTILE
NTILE(n) divides the ordered rows in a window into n numbered groups, called buckets. The buckets are as equal in size as possible.
SELECT
salesperson,
amount,
NTILE(4) OVER (ORDER BY amount DESC) AS quartile
FROM sales;This query sorts sales from largest to smallest and assigns each sale to one of four buckets. The first bucket contains the highest values, so it represents the top quarter of the rows.
If the number of rows does not divide evenly, earlier buckets receive one extra row. For example, 10 rows divided into four buckets produce sizes of 3, 3, 2, and 2.
NTILE assigns rows based on their position. It does not guarantee that equal values stay in the same bucket. Add a unique column to the window ordering when you need deterministic results:
NTILE(4) OVER (ORDER BY amount DESC, id) AS quartileMeasuring relative position with PERCENT_RANK
PERCENT_RANK() returns a relative rank between 0 and 1. Its calculation is:
(rank - 1) / (number of rows - 1)The first row always has a percent rank of 0. The last rank usually has a value of 1.
SELECT
salesperson,
amount,
RANK() OVER (ORDER BY amount DESC) AS rnk,
PERCENT_RANK() OVER (ORDER BY amount DESC) AS percent_rank
FROM sales;Because PERCENT_RANK uses RANK, tied values receive the same result. Gaps after ties also affect later values.
For a single-row window, the denominator would be zero. Database systems handle this case according to their implementation, so test it if your query can produce one-row partitions.
Measuring the cumulative distribution with CUME_DIST
CUME_DIST() returns the fraction of rows whose ordered value is less than or equal to the current row's value. With descending order, the first row represents the highest values, so the result indicates the fraction at or above that value.
SELECT
salesperson,
amount,
CUME_DIST() OVER (ORDER BY amount DESC) AS cumulative_fraction
FROM sales;Unlike PERCENT_RANK, CUME_DIST includes all rows tied with the current row. This means its result can jump by more than one row's fraction.
| Function | Main question | Ties receive the same result? |
|---|---|---|
| `NTILE(4)` | Which quarter contains this row? | Not necessarily |
| `PERCENT_RANK()` | What is this row's relative rank? | Yes |
| `CUME_DIST()` | What fraction of rows have reached this value? | Yes |
Choosing the ordering direction
The direction changes the meaning of every result. With ORDER BY amount DESC, high amounts come first. With ORDER BY amount ASC, low amounts come first.
A useful check is to ask what the first row should represent:
- Highest value: use
DESC. - Lowest value: use
ASC. - A percentile for “at or below this value”: usually use
ASCwithCUME_DIST.
Partitioning distributions
Add PARTITION BY to calculate a separate distribution for each group:
SELECT
region,
salesperson,
amount,
NTILE(4) OVER (
PARTITION BY region
ORDER BY amount DESC
) AS regional_quartile
FROM sales;Each region starts again at bucket 1. Without PARTITION BY, all regions compete in one distribution.
Exercise
Which function should you use to divide each region's sales into four groups with roughly equal numbers of rows?
Show answer
Use NTILE(4) with PARTITION BY region and an ORDER BY clause.