Distribution & Bucketing Functions: NTILE(), PERCENT_RANK(), CUME_DIST()
Bucket and score distributions with NTILE, PERCENT_RANK, and CUME_DIST.
In plain words
NTILE cuts a sorted list into equal piles. PERCENT_RANK and CUME_DIST tell you how high up the list each row is, as a number between 0 and 1.
Think of it like…
Lining up a class by height: NTILE(4) splits the line into four equal groups; PERCENT_RANK says how far along the line each student stands (0 for the first, 1 for the last); CUME_DIST says what share of the class is this tall or shorter.
Where you’ve seen it
Customer segmentation: SELECT customer_id, spend, NTILE(4) OVER (ORDER BY spend DESC) AS quartile FROM customer_spend;. Quartile 1 is the top 25% of spenders, which marketing targets with a loyalty offer.
Words you’ll hear
- NTILE(n)
- Bucket number 1…n; bucket sizes differ by at most one row.
- PERCENT_RANK
- (rank − 1) / (total rows − 1); the first row is 0.
- CUME_DIST
- (rows with value ≤ current) / total rows; the last row is 1.
- PERCENTILE_CONT / DISC
- Ordered-set aggregates for medians and percentiles: interpolated or actual value.
Visual diagram
Theory & example queries
NTILE for equal-sized buckets
NTILE(n) numbers rows 1 to n so each bucket gets roughly the same number of rows; if the rows don't divide evenly, the first buckets get one extra row. It's based purely on position in the order, so tied values can end up in different buckets, and buckets say nothing about value ranges.
That makes NTILE right for "split customers into four equal groups", and wrong for "customers who spent ₹0–1,000, ₹1,000–5,000 …". For value-based ranges use CASE, or PostgreSQL's WIDTH_BUCKET(value, low, high, count).
SELECT customer_id, country, NTILE(4) OVER ( PARTITION BY country ORDER BY spend DESC ) AS quartileFROM customer_spend;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output4 sample rows
| customer_id | country | quartile |
|---|---|---|
| 104 | India | 1 |
| 118 | India | 2 |
| 202 | Singapore | 1 |
| 219 | Singapore | 2 |
Note: Illustrative customers and buckets; the spend ordering and partition size determine each NTILE value.
Key takeaway: NTILE = equal row counts, not equal value ranges.
Relative position and percentiles
PERCENT_RANK and CUME_DIST both express position as a fraction. Use CUME_DIST for "top 10%" filters: CUME_DIST() OVER (ORDER BY spend DESC) <= 0.10 keeps the top tenth, and tied rows always get the same value, so a tie at the boundary is treated consistently. PERCENT_RANK starts at exactly 0, which suits "percentile score" displays.
For a single percentile of a group, such as the median, use the ordered-set aggregates: PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY amount) interpolates between the two middle values, and PERCENTILE_DISC returns an actual value from the data. They're supported in PostgreSQL, SQL Server (as window functions), Oracle and Snowflake; MySQL has no built-in median.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT date_trunc('month', created_at) AS MONTH, PERCENTILE_CONT(0.5) WITHIN GROUP ( ORDER BY amount ) AS median, PERCENTILE_CONT(0.9) WITHIN GROUP ( ORDER BY amount ) AS p90FROM ordersGROUP BY 1;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output3 sample rows
| month | median | p90 |
|---|---|---|
| 2026-09-01 | 480 | 1520 |
| 2026-10-01 | 525 | 1680 |
| 2026-11-01 | 510 | 1590 |
Note: Illustrative continuous percentiles for monthly order amounts in currency units.
Key takeaway: CUME_DIST for top-X% filters; PERCENTILE_CONT for medians.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Decide what you need
Equal-sized groups means NTILE. A position score means PERCENT_RANK or CUME_DIST. A single statistic like the median means PERCENTILE_CONT.
quartiles · top 10% · median
When to use it & trade-offs
What it is
NTILE(n) splits ordered rows into n buckets of (almost) equal size, such as quartiles with NTILE(4). PERCENT_RANK() gives each row's relative rank from 0 to 1: (rank − 1) / (rows − 1). CUME_DIST() gives the fraction of rows with a value less than or equal to the current one, from just above 0 up to 1. For medians and other percentiles, use PERCENTILE_CONT or PERCENTILE_DISC.
Why it matters: "Top 10% of customers by spend", "split users into quartiles" and "what's the median order value?" come up constantly in analytics, segmentation and interviews. Each function answers a slightly different question, and NTILE in particular behaves unexpectedly with ties.
When to reach for it
- Customer and product segmentation by quartile or decile
- Selecting the top 10% by spend or activity
- Medians and latency percentiles in reports
When to avoid or be careful
- NTILE for value-range buckets
- Using AVG when the distribution is skewed and the median is what matters
- Ignoring how ties fall at bucket boundaries
NTILE vs PERCENT_RANK / CUME_DIST vs PERCENTILE_CONT
Strengths: Simple bucket numbers for every row.
Watch out: Ties can be split; buckets aren't value ranges.
Strengths: Ties always get the same value.
Watch out: Fractions, not bucket labels.
Strengths: Exact statistic per group.
Watch out: Returns one value per group, not per row; not in MySQL.
Interview signals & numbers to know
When this topic comes up
- “Split customers into quartiles”
- “Top 10% of users by spend”
- “What's the median order value?”
- p95 / p99 latency
- Percentile rank
Benchmarks & rules of thumb
- 0 to 1
- range of PERCENT_RANK (first row is 0)
- (0, 1]
- range of CUME_DIST (last row is 1)
- 0.5
- the median in PERCENTILE_CONT
Common mistakes & follow-ups
Common interview mistakes
- Using NTILE(10) and assuming each decile covers an equal range of values.
- Filtering "top 10%" with NTILE and getting inconsistent results when values tie at the boundary.
- Reporting the average order value for a skewed distribution where the median tells the real story.
Likely interviewer follow-ups
- What's the difference between PERCENT_RANK and CUME_DIST?
- How would you compute the median in PostgreSQL, and in MySQL?
- How does NTILE handle 10 rows split into 3 buckets?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Distribution & Bucketing Functions: NTILE(), PERCENT_RANK(), CUME_DIST() with hands-on SQL practice and interview Q&A.