Window Functions

NTILE: Dividing Data into Buckets

Senior Data Analyst
March 7, 2026
5 min read

The Quartile Report

Marketing wanted to segment customers into 4 tiers based on their spend: Top 25%, Second 25%, etc.

The NTILE Function

`NTILE(n)` divides the ordered partition into `n` equal-sized buckets.

SELECT

customer_id,

total_spend,

NTILE(4) OVER (ORDER BY total_spend DESC) AS spend_quartile

FROM customer_spend;

The Result

  • Quartile 1: Top 25% spenders.
  • Quartile 4: Bottom 25% spenders.
  • Pro Tip

    Use `NTILE(100)` to calculate percentiles!

    *Day 67: Percent Rank and Cumulative Distribution.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.