Chapter 9 · Interview Level
34. RFM Customer Segmentation
Perform RFM (Recency, Frequency, Monetary) analysis. For each customer, compute: Recency = days since their last order (from '2025-01-01'), Frequency = number of orders, Monetary = total spend. Then assign each metric a score 1-3 using NTILE(3) (higher = better: more recent, more frequent, more spend). Break ties within each NTILE by customer_id so scores are deterministic. Return customer_id, recency_days, frequency, monetary, and their three scores. Sort by monetary descending.
Tables
- PKidINT
- FKcustomer_id→ customers.idINT
- order_dateTEXT
- statusTEXT
- total_amountREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.