RFM Customer Segmentation with NTILE
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Build an RFM (recency, frequency, monetary) segmentation from the `orders` table, using `2024-04-01` as the reference date. Give each customer a 1-to-4 score on each of the three metrics with `NTILE(4)`, add the scores into a total, and map the total to a segment label.
```hint Which end gets 4
`NTILE` gives bucket 1 to the first rows of its ordering. For each metric, check which sort direction puts the best customers last, and remember that a small `recency_days` is the good end.
```
### Input Table(s)
Table `orders`, primary key `order_id`:
| Column | Type | Meaning |
|---|---|---|
| `order_id` | INT | Primary key, one row per order |
| `customer_id` | INT | Customer who placed the order; a customer can have many orders |
| `order_date` | DATE | Date of the order |
| `amount` | DECIMAL(10,2) | Order value |
### Output Contract
Return one row per customer with these columns, in this order:
| Column | Meaning |
|---|---|
| `customer_id` | The customer |
| `recency_days` | Days from the customer's latest `order_date` to `2024-04-01` (integer) |
| `frequency` | Number of orders |
| `monetary` | Total `amount`, rounded to 2 decimals |
| `r_score` | `NTILE(4)` over all customers ordered by `recency_days` descending, then `customer_id` ascending |
| `f_score` | `NTILE(4)` over all customers ordered by `frequency` ascending, then `customer_id` ascending |
| `m_score` | `NTILE(4)` over all customers ordered by `monetary` ascending, then `customer_id` ascending |
| `rfm_total` | `r_score + f_score + m_score` |
| `segment` | `'Champions'` when `rfm_total >= 10`, else `'Loyal'` when `>= 7`, else `'At Risk'` when `>= 4`, else `'Lost'` |
- A higher score is better on every metric: more recent, more orders, more spend. The sort directions above put the best customers in bucket 4.
- Order the rows by `rfm_total` descending, then `customer_id` ascending. `customer_id` is unique per output row, so this order is unique.
### Example
Input `orders`:
| order_id | customer_id | order_date | amount |
|---|---|---|---|
| 1 | 301 | 2024-03-30 | 50.00 |
| 2 | 301 | 2024-03-25 | 150.00 |
| 3 | 301 | 2024-02-10 | 200.00 |
| 4 | 302 | 2024-01-01 | 900.00 |
| 5 | 303 | 2024-03-15 | 20.00 |
| 6 | 303 | 2024-03-01 | 30.00 |
| 7 | 304 | 2023-12-01 | 10.00 |
| 8 | 305 | 2024-03-30 | 120.00 |
Expected output:
| customer_id | recency_days | frequency | monetary | r_score | f_score | m_score | rfm_total | segment |
|---|---|---|---|---|---|---|---|---|
| 301 | 2 | 3 | 400.00 | 3 | 4 | 3 | 10 | Champions |
| 305 | 2 | 1 | 120.00 | 4 | 2 | 2 | 8 | Loyal |
| 302 | 91 | 1 | 900.00 | 1 | 1 | 4 | 6 | At Risk |
| 303 | 17 | 2 | 50.00 | 2 | 3 | 1 | 6 | At Risk |
| 304 | 122 | 1 | 10.00 | 1 | 1 | 1 | 3 | Lost |
Five customers split into tiles of 2, 1, 1 and 1. For recency the order is 304 (122 days), 302 (91), 303 (17), 301 (2), 305 (2), with 301 ahead of 305 on the id tiebreak, so 304 and 302 score 1 and 305 scores 4. For frequency the order is 302, 304, 305 (one order each, by id), then 303 and 301. For monetary it is 304, 303, 305, 301, 302. Customers 302 and 303 tie at 6 and are listed by `customer_id`.
### Constraints and Clarifications
- The table has 1 to 10,000 orders, and `amount` is between 0 and 1,000,000.
- Every `order_date` is on or before `2024-04-01`, so `recency_days` is never negative.
- No column contains NULL.
- Every order counts toward `frequency`, including orders with amount 0 and several orders on the same day.
- Ties on a metric are broken by `customer_id` ascending, so customers with identical metrics can get different scores. Four customers with the same recency, frequency and spend score 1, 2, 3 and 4 in id order.
- `NTILE(4)` makes the buckets as equal as possible and gives the extra rows to the lowest buckets. With fewer than four customers only the low scores appear: a single customer scores 1 on every metric and lands in `'Lost'`.
Overview: Write a SQL query that scores each customer on recency, frequency and monetary value with NTILE quartiles, sums the three scores and assigns a segment label. It tests per-customer aggregation, the sort direction of window functions and deterministic tie-breaking.