SQL
All Top 50 SQL Interview Questions#47
Cohort Segmentation Using NTILE() Quartiles in SQL
MediumAmazonInterview Question #47
Asked at AmazonGiven `customer_spend` (customer_id, annual_spend), divide customers into 4 equal quartiles using `NTILE(4)`. Assign 'Top 25% (Q4)' to tier 4.
Input Table: customer_spend
4 rows preview| customer_id | annual_spend |
|---|---|
| 1 | 50000 |
| 2 | 35000 |
| 3 | 20000 |
| 4 | 5000 |
Expected Output Structure4 rows
| customer_id | annual_spend | spend_quartile |
|---|---|---|
| 4 | 5000 | 1 |
| 3 | 20000 | 2 |
| 2 | 35000 | 3 |
| 1 | 50000 | 4 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.