intermediate

NTILE Function

5 min readLast updated: 2026-07-23

Overview

NTILE(buckets) divides an ordered partition into a specified number of nearly equal-sized buckets, returning the bucket number for each row.

Learning Objectives

  • Divide records into percentiles, quartiles (4), or deciles (10) using NTILE().
  • Segment customers or performance metrics into target tiers.

Detailed Concept Explanation

NTILE(N) assigns a bucket number from 1 to $N$ to each row. If the number of rows is not evenly divisible by $N$, the extra rows are distributed to the first buckets.


Code Examples

SQL

sql
-- Segment customers into 4 spending quartiles (Quartile 1 = Top Spenders)
SELECT 
    customer_id,
    total_spent,
    NTILE(4) OVER (ORDER BY total_spent DESC) AS spending_quartile
FROM customer_stats;

Summary

NTILE(N) splits ordered partition rows into $N$ equal-sized bucket groups.