0PricingLogin
SQL Academy · Lesson

Bucketing with NTILE and Cume_Dist

Split rows into N equal-size buckets with NTILE, and compute cumulative distribution with CUME_DIST and PERCENT_RANK.

NTILE: Equal-Size Buckets

NTILE(n) splits ordered rows into n roughly-equal buckets:

SELECT id, total,
       NTILE(4) OVER (ORDER BY total) AS quartile
FROM orders;
-- 1 = bottom 25%, 2 = next, 3, 4 = top 25%

Percentile Buckets

NTILE(10) for deciles, NTILE(100) for percentiles:

SELECT id, score,
       NTILE(100) OVER (ORDER BY score DESC) AS percentile
FROM students;
-- percentile 1 = top 1%

All lessons in this course

  1. Frame Clauses: ROWS vs RANGE
  2. Lag/Lead with Frame Windows
  3. Bucketing with NTILE and Cume_Dist
  4. Real-World Reporting Patterns
← Back to SQL Academy