Stop 08 · Window FunctionsTopics · 8/9

NTILE and Percentiles

Part of the SQL Roadmap

In short

Marketing wants the top 25% of spenders, and finance wants the median order value. NTILE and the percentile functions turn "split them into groups" and "find the middle" into one line each.

NTILE(n) splits ordered rows into n roughly equal buckets, such as quartiles. Percentile functions like PERCENT_RANK or PERCENTILE_CONT help find medians and cut-offs.

The jaadu sorts a long row of golden mangoes by size into four wooden crates lined up on a farm cart.

Think of it like this

Grading mangoes into crates

A Ratnagiri farmer lines up 100 Alphonso mangoes from biggest to smallest, then fills four crates with 25 each. Crate 1 has the biggest. That's NTILE(4). The mango sitting exactly in the middle of the line is the median, which is what PERCENTILE_CONT(0.5) finds.

Watch it happen

NTILE(4) on 10 customers sorted by spend

9000
0
7500
1
6000
2
5200
3
4100
4
3500
5
2800
6
2000
7
1200
8
600
9

1/5Ten customers sorted by total spend in rupees, highest first.

The big ideas

What you need to know

NTILE(n) makes buckets

It splits ordered rows into n groups of nearly equal size and labels them 1 to n.

PERCENT_RANK

Gives each row a position from 0 to 1. A value of 0.9 means 90% of rows rank below it.

PERCENTILE_CONT for the median

PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY amount) returns the median, interpolating between middle values.

Equal counts, not equal ranges

NTILE gives each bucket the same number of rows. The spend range inside each bucket can be very different.

See it in code

Walk through the code

sql
1SELECT2  customer_id,3  total_spend,4  NTILE(4) OVER (ORDER BY total_spend DESC) AS spend_quartile,5  ROUND(6    PERCENT_RANK() OVER (ORDER BY total_spend)::numeric,7    28  ) AS pct_rank9FROM customer_totals;10 11SELECT12  PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY total_spend) AS median_spend13FROM customer_totals;

1/3Highest spenders land in bucket 1.

Quartiles and percent rank per customer, then the overall median. NTILE and PERCENT_RANK work in MySQL 8+ and SQL Server too. MySQL has no PERCENTILE_CONT, and SQL Server only allows it with OVER.

Save yourself the pain

Common mistakes

Oops

Expecting NTILE(4) on 10 rows to give 2.5 rows per bucket.

Do this instead

Buckets hold whole rows. The first buckets get the extras, so you get 3, 3, 2, 2.

Oops

Using AVG when the boss asked for a typical order value.

Do this instead

One huge wedding order can drag the average up. The median is often a fairer 'typical' value.

Your mission

Find your top spenders

~20 min

Quick check

NTILE(4) is run over 10 rows. What are the bucket sizes?

Go deeper

  1. 1Split customers into 4 spending quartiles with NTILE(4).
  2. 2Find the median order value using your database's percentile function.
  3. 3Note that MySQL has no built-in median, so learn a workaround.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge