Segment Customers into Quartiles by Total Spend
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
For every customer, compute `total_spend` (the sum of their order amounts) and assign `spend_quartile` with `NTILE(4)`. Return `customer_id`, `customer_name`, `total_spend` and `spend_quartile`.
Quartile 1 holds the highest spenders and quartile 4 the lowest. Break spend ties by the smaller `customer_id` first. Customers with no orders, and orders whose `amount` is NULL, contribute 0. Round `total_spend` to 2 decimals.
```hint Missing customers
Check which join keeps a customer who never ordered, and what `SUM` returns for that customer.
```
### Input Table(s)
`customers`
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Customer id |
| customer_name | VARCHAR(100) | Customer name, never NULL |
Primary key: `customer_id`.
`orders`
| Column | Type | Meaning |
|---|---|---|
| order_id | INT | Order id |
| customer_id | INT | Customer who placed the order |
| amount | DECIMAL(10,2) | Order amount, can be NULL |
Primary key: `order_id`. Foreign key: `orders.customer_id` references `customers.customer_id`.
### Output Contract
One row per customer, with these columns in this order:
| Column | Meaning |
|---|---|
| customer_id | Customer id |
| customer_name | Customer name |
| total_spend | Sum of the customer's order amounts, 0 when there is nothing to sum, rounded to 2 decimals |
| spend_quartile | `NTILE(4)` bucket over all customers ordered by `total_spend` descending, then `customer_id` ascending |
Order the rows by `total_spend` descending, then `customer_id` ascending.
### Example
`customers`
| customer_id | customer_name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Carol |
| 4 | Dave |
`orders`
| order_id | customer_id | amount |
|---|---|---|
| 1 | 1 | 150.50 |
| 2 | 1 | 49.50 |
| 3 | 2 | 100.00 |
| 4 | 4 | NULL |
Expected result:
| customer_id | customer_name | total_spend | spend_quartile |
|---|---|---|---|
| 1 | Alice | 200.00 | 1 |
| 2 | Bob | 100.00 | 2 |
| 3 | Carol | 0.00 | 3 |
| 4 | Dave | 0.00 | 4 |
Alice totals 150.50 + 49.50 = 200.00 and Bob totals 100.00. Carol has no orders and Dave's only order has a NULL amount, so both total 0.00, and Carol comes first because her id is smaller. With 4 customers, `NTILE(4)` puts one customer in each quartile.
### Constraints and Clarifications
- There are 1 to 10,000 customers. `customer_id` and `order_id` are unique.
- A customer can have any number of orders, including none. All of a customer's orders are summed into one row.
- A NULL `amount` adds nothing to the sum. A customer with no orders, or with only NULL amounts, has `total_spend` 0.00.
- When the number of customers is not divisible by 4, the lower-numbered quartiles take one extra customer each, as `NTILE` does: 5 customers split 2, 1, 1, 1 and 6 customers split 2, 2, 1, 1. With fewer than 4 customers, only quartiles 1 up to the customer count appear.
- Customers with equal spend can land in different quartiles. The one with the smaller `customer_id` takes the earlier position.
Overview: Write a SQL query that totals each customer's order spend, including customers with no orders, and assigns spend quartiles with NTILE(4). It tests LEFT JOIN aggregation, COALESCE for NULL totals, and a deterministic window ordering that breaks ties.