Expert

PERCENT_RANK & NTILE: Segmenting Your Data

SQL Mastery Team
March 30, 2026
5 min read

Welcome to **Day 68**. Today we learn how to answer the question: "Which bucket does this row fall into?"

Percentiles: PERCENT_RANK()

`PERCENT_RANK` returns a number between 0 and 1 representing where a row sits as a percentage.

  • 0.99 = Top 1%.
  • 0.50 = Median.
  • SELECT

    name,

    salary,

    PERCENT_RANK() OVER(ORDER BY salary) as salary_percentile

    FROM employees;

    Bucketing: NTILE()

    `NTILE(n)` slices your data into `n` equal groups. This is perfect for the marketing team who wants to send different emails to:

  • **Tier 1 (Top 25%)**: Big Spender discount.
  • **Tier 4 (Bottom 25%)**: "We miss you" discount.
  • SELECT

    customer_id,

    NTILE(4) OVER(ORDER BY total_spent DESC) as customer_tier

    FROM customers;

    Use Case: Grades

    If you have 100 students and you want to give A's to the top 10, use `NTILE(10)`. The first bucket contains your straight-A students!

    Your Task for Today

    Segment your product catalog into 10 price "Deciles" using `NTILE(10)`.

    *Day 69: FIRST_VALUE & LAST_VALUE—Retrieving endpoints.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.