SQL Window Functions and CTEs/Bucket rows with NTILE

Divide ordered rows into a fixed number of groups.

Section: Buckets and Percentiles

Bucket rows with NTILE

sql
sql
SELECT customer_id, total_spent,
       NTILE(4) OVER (ORDER BY total_spent DESC) AS quartile
FROM customer_lifetime_value;
Explanation

NTILE is helpful for quartiles, deciles, and score bands.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Buckets and Percentiles
Use PERCENT_RANK and CUME_DIST
Measure relative row position inside an ordered set.
OpenIn sheetsqlsame section
Assign row numbers
Number rows within an ordered partition.
OpenIn sheetsql1 tag match
Use LAG
Read a value from the previous row in the same partition.
OpenIn sheetsql1 tag match
Rank rows
Assign ordered rankings with or without gaps.
OpenIn sheetsql1 tag match
Use LEAD
Read a value from the next row in the same partition.
OpenIn sheetsql1 tag match
Compute running total
Accumulate values across an ordered window.
OpenIn sheetsql1 tag match