PERCENT_RANK & NTILE: Segmenting Your Data
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.
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:
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.*