Top 3 Customers by Total Spend in a Date Range
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
You have two tables, `customers` and `orders`. Return the top 3 customers by total spend, counting only orders with `order_date` on or after `'2024-01-01'` and strictly before `'2024-01-31'`. Output `customer_id`, `name` and `total_spend` (rounded to 2 decimal places). Ties on total spend are broken by smaller `customer_id` first.
```hint Order of operations
Decide which rows to drop before you add anything up, and make sure the sort fully decides the order before you cut the list to three rows.
```
### Input Table(s)
Table `customers`, primary key `customer_id`:
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Customer identifier (primary key) |
| name | VARCHAR(100) | Customer name, NOT NULL |
Table `orders`, primary key `order_id`. `customer_id` is a foreign key to `customers.customer_id`:
| Column | Type | Meaning |
|---|---|---|
| order_id | INT | Order identifier (primary key) |
| customer_id | INT | Customer who placed the order |
| amount | DECIMAL(10,2) | Order amount, positive, NOT NULL |
| order_date | DATE | Date of the order, NOT NULL |
### Output Contract
- Columns, in this order: `customer_id`, `name`, `total_spend`.
- `total_spend` is the sum of `amount` over the customer's orders inside the window, rounded to 2 decimal places.
- Sort by `total_spend` descending, then by `customer_id` ascending.
- Return at most 3 rows. When fewer than 3 customers have an order in the window, return only those customers.
### Example
`customers`
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Chen |
| 4 | Dana |
| 5 | Eli |
`orders`
| order_id | customer_id | amount | order_date |
|---|---|---|---|
| 101 | 1 | 120.00 | 2024-01-05 |
| 102 | 2 | 200.00 | 2024-01-10 |
| 103 | 1 | 80.00 | 2024-01-20 |
| 104 | 3 | 150.00 | 2024-01-31 |
| 105 | 3 | 90.50 | 2024-01-15 |
| 106 | 4 | 500.00 | 2023-12-31 |
| 107 | 4 | 60.25 | 2024-01-02 |
| 108 | 5 | 90.50 | 2024-01-30 |
Expected result:
| customer_id | name | total_spend |
|---|---|---|
| 1 | Alice | 200.00 |
| 2 | Bob | 200.00 |
| 3 | Chen | 90.50 |
Order 104 is dated `2024-01-31` and order 106 is dated `2023-12-31`, so both fall outside the window. The remaining totals are Alice 200.00, Bob 200.00, Chen 90.50, Eli 90.50 and Dana 60.25. Alice and Bob tie, and so do Chen and Eli. The smaller `customer_id` wins each tie, so Eli (5) misses the cut.
### Constraints and Clarifications
- `customers` has at most 10,000 rows and `orders` at most 100,000 rows.
- `amount` is a positive decimal and `order_date` is a valid date. Neither column contains NULLs.
- Every `customer_id` in `orders` references a row in `customers`.
- The window is half-open. Orders dated `2024-01-31` are excluded, along with anything before `2024-01-01`.
- A customer with several orders in the window gets one row holding the sum. Customers with no order in the window are left out.
- Ties on `total_spend` are broken by smaller `customer_id`. Because `customer_id` is the primary key, the order of the result is unique.
Overview: Write a SQL query that returns the three customers with the highest total order amount inside a half-open January 2024 date window, breaking ties by customer id. It tests joining two tables, filtering dates before aggregating, SUM with GROUP BY, rounding, and deterministic ordering with LIMIT.