SQLSQL
#47

Cohort Segmentation Using NTILE() Quartiles in SQL

MediumAmazon
Interview Question #47
Asked at Amazon

Given `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_idannual_spend
150000
235000
320000
45000
Expected Output Structure4 rows
customer_idannual_spendspend_quartile
450001
3200002
2350003
1500004
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us