Coding question · Top N Per GroupVery HardAsked at Airbnb

Bucket customers into spend quartiles

NTILE on an aggregated CTE.

Back to all coding questions

The Brief

CRM teamMarketing Ops#coding-interview

Rank customers by total lifetime spend and bucket them into 4 equal-sized quartiles (quartile 1 = highest spenders). Columns: `customer_id`, `total_spend`, `spend_quartile`. Order by total_spend descending, then customer_id. Table: `ci_orders`.

Tables & columns available

ci_ordersfact6 columns
ColumnTypeKey
order_idINTPK
customer_idINT
product_nameTEXT
categoryTEXT
order_dateDATE
revenueNUMERIC

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Aggregate per-customer SUM(revenue) in a CTE.

Hint 2

NTILE(4) OVER (ORDER BY total_spend DESC) in the outer SELECT.

Hint 3

With 8 customers, each quartile has exactly 2.