NTILE
Split ordered rows into N equal-sized buckets: quartiles (NTILE(4)), deciles (NTILE(10)), percentiles (NTILE(100)). If the row count doesn't divide evenly, the first (N mod B) buckets each get one extra row.
Syntax #
sql
SELECT key_col, metric,
NTILE(4) OVER (ORDER BY metric DESC) AS quartile
FROM table_name;Example #
Loading SQL editor...
Warning
**NTILE buckets by row count, not by value range.** Two rows with the same metric can land in different buckets because one had to "fit" into the smaller tile. If the business wants "top quartile by spend" as "customers who spent $500+", NTILE gives you the wrong answer. Use a CASE with explicit thresholds (or PERCENT_RANK) instead.