Summarize each customer's spending and assign a loyalty tier from orders, order items and products
Company: IBM
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
An online store keeps its customers, products, orders and the line items of each order in four tables. For each customer, return `customer_id`, `customer_name` and a set of purchase metrics, including a loyalty tier based on total spending. Every customer must appear in the result, including customers who have never placed an order.
```hint Watch the row counts
After joining an order to its line items, the order appears once per line item. Decide which of your metrics that repetition would distort and which it would not.
```
### Input Table(s)
`customers`
| column | type | meaning |
|---|---|---|
| `customer_id` | INTEGER | Unique id of the customer. Primary key. |
| `name` | VARCHAR(100) | Customer's name. Not null. |
`products`
| column | type | meaning |
|---|---|---|
| `product_id` | INTEGER | Unique id of the product. Primary key. |
| `category` | VARCHAR(50) | Product category, such as `Electronics`. Not null. |
| `price` | NUMERIC(10,2) | Current list price. Not null. |
`orders`
| column | type | meaning |
|---|---|---|
| `order_id` | INTEGER | Unique id of the order. Primary key. |
| `customer_id` | INTEGER | Customer who placed the order. Foreign key to `customers`. Not null. |
| `order_date` | DATE | Date the order was placed. Not null. |
| `status` | VARCHAR(20) | One of `'pending'`, `'shipped'`, `'delivered'`, `'cancelled'`. Not null. |
`order_items`
| column | type | meaning |
|---|---|---|
| `order_item_id` | INTEGER | Unique id of the line item. Primary key. |
| `order_id` | INTEGER | Order the line belongs to. Foreign key to `orders`. Not null. |
| `product_id` | INTEGER | Product on the line. Foreign key to `products`. Not null. |
| `quantity` | INTEGER | Units bought on this line. Not null. |
| `unit_price` | NUMERIC(10,2) | Price charged per unit on this order. Not null. |
### Output Contract
One row per customer, with these columns in this order:
- `customer_id`
- `customer_name`: the customer's `name`.
- `total_orders`: number of distinct orders the customer placed.
- `total_spent`: sum of `quantity * unit_price` over all of the customer's line items, to 2 decimal places.
- `avg_order_value`: `total_spent` divided by `total_orders`, rounded to 2 decimal places.
- `last_order_date`: the most recent `order_date`.
- `unique_categories_purchased`: number of distinct product categories across the customer's line items.
- `loyalty_tier`: `'Platinum'` when `total_spent` is at least 1000, `'Gold'` when it is at least 500, `'Silver'` when it is at least 100, and `'Bronze'` below 100.
Sort by `total_spent` descending, then by `customer_id` ascending.
### Example
`customers`
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Chen |
| 4 | Dana |
| 5 | Eve |
`products`
| product_id | category | price |
|---|---|---|
| 101 | Electronics | 300.00 |
| 102 | Books | 20.00 |
| 103 | Home | 45.50 |
| 104 | Electronics | 80.00 |
`orders`
| order_id | customer_id | order_date | status |
|---|---|---|---|
| 1001 | 1 | 2024-01-05 | delivered |
| 1002 | 1 | 2024-02-10 | shipped |
| 1003 | 2 | 2024-01-20 | delivered |
| 1004 | 2 | 2024-03-02 | cancelled |
| 1005 | 3 | 2024-02-14 | pending |
| 1006 | 5 | 2024-03-10 | pending |
`order_items`
| order_item_id | order_id | product_id | quantity | unit_price |
|---|---|---|---|---|
| 1 | 1001 | 101 | 3 | 300.00 |
| 2 | 1001 | 102 | 3 | 20.00 |
| 3 | 1002 | 104 | 1 | 75.00 |
| 4 | 1003 | 103 | 2 | 45.50 |
| 5 | 1004 | 102 | 1 | 20.00 |
| 6 | 1005 | 101 | 2 | 280.00 |
Expected result:
| customer_id | customer_name | total_orders | total_spent | avg_order_value | last_order_date | unique_categories_purchased | loyalty_tier |
|---|---|---|---|---|---|---|---|
| 1 | Alice | 2 | 1035.00 | 517.50 | 2024-02-10 | 2 | Platinum |
| 3 | Chen | 1 | 560.00 | 560.00 | 2024-02-14 | 1 | Gold |
| 2 | Bob | 2 | 111.00 | 55.50 | 2024-03-02 | 2 | Silver |
| 4 | Dana | 0 | 0.00 | 0.00 | NULL | 0 | Bronze |
| 5 | Eve | 1 | 0.00 | 0.00 | 2024-03-10 | 0 | Bronze |
Alice spent 900.00 + 60.00 + 75.00 = 1035.00 over 2 orders. Her line for product 104 is charged at its order price of 75.00, not the list price of 80.00. Her products fall into Electronics and Books, so she has 2 categories. Bob's cancelled order 1004 still counts, which gives him 2 orders, 111.00 spent and a last order date of 2024-03-02. Dana has no orders, so her numeric fields are 0 and her last order date is NULL. Eve has one order with no line items, so it counts as an order with zero spend. Dana and Eve tie at 0.00 and are ordered by `customer_id`.
### Constraints and Clarifications
- `customers` and `products` each have 1 to 10,000 rows. `orders` and `order_items` each have 0 to 100,000 rows.
- `unit_price` and `price` are positive with up to 2 decimal places, and `quantity` is at least 1.
- Spending uses `order_items.unit_price`, the price charged on the order. `products.price` is not used in any metric.
- Every order counts toward the metrics whatever its `status`, including `'cancelled'`.
- A customer with no orders gets 0 for `total_orders`, `total_spent`, `avg_order_value` and `unique_categories_purchased`, NULL for `last_order_date`, and the tier `'Bronze'`.
- An order with no line items counts as an order with zero spend.
- Tier thresholds are inclusive: exactly 1000.00 is `'Platinum'`, exactly 500.00 is `'Gold'`, exactly 100.00 is `'Silver'`.
- Customers with equal `total_spent` are ordered by `customer_id` ascending.
Overview: Write a SQL query that joins customers, orders, order items and products to report each customer's order count, total and average spend, last order date, category count and loyalty tier. It tests left join chains, distinct counts under join fan-out, NULL defaults for customers without orders, and conditional tiers.