Top 3 Whale Customers by Total Spend in 2024
Company: Qualcomm
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
You have a `customers` table and an `orders` table. Find the top 3 customers by total spend on orders placed from 2024-01-01 to 2024-12-31 (inclusive). Return `customer_id`, `customer_name`, and `total_spend`, ordered by `total_spend` descending, then `customer_id` ascending.
```hint Boundary days
Both ends of the range count. Check what your date condition does with an order placed on January 1 and one placed on December 31.
```
### Input Table(s)
Table `customers`, primary key `customer_id`:
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Customer identifier (primary key) |
| customer_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, NOT NULL |
| amount | DECIMAL(10,2) | Order amount, positive, NOT NULL |
| order_date | DATE | Date the order was placed, NOT NULL |
### Output Contract
- Columns, in this order: `customer_id`, `customer_name`, `total_spend`.
- `total_spend` is the sum of `amount` over the customer's orders dated in 2024. Because `amount` has 2 decimal places, the sum does too, and no extra rounding is needed.
- Sort by `total_spend` descending, then by `customer_id` ascending.
- Return at most 3 rows. When fewer than 3 customers ordered in 2024, return only those customers.
### Example
`customers`
| customer_id | customer_name |
|---|---|
| 1 | Ava |
| 2 | Ben |
| 3 | Chloe |
| 4 | Dmitri |
| 5 | Elena |
`orders`
| order_id | customer_id | amount | order_date |
|---|---|---|---|
| 1 | 1 | 300.00 | 2024-01-01 |
| 2 | 2 | 250.00 | 2024-06-15 |
| 3 | 3 | 400.00 | 2023-12-31 |
| 4 | 3 | 175.00 | 2024-12-31 |
| 5 | 4 | 175.00 | 2024-03-10 |
| 6 | 4 | 100.00 | 2025-01-01 |
| 7 | 5 | 90.00 | 2024-08-01 |
| 8 | 2 | 50.00 | 2024-11-20 |
Expected result:
| customer_id | customer_name | total_spend |
|---|---|---|
| 1 | Ava | 300.00 |
| 2 | Ben | 300.00 |
| 3 | Chloe | 175.00 |
Orders 3 (2023-12-31) and 6 (2025-01-01) fall outside 2024. Orders 1 and 4 sit on the two boundary days and count. The 2024 totals are Ava 300.00, Ben 300.00 (250.00 + 50.00), Chloe 175.00, Dmitri 175.00 and Elena 90.00. Chloe and Dmitri tie for third, and Chloe takes the slot because her `customer_id` is smaller.
### Constraints and Clarifications
- `customers` has at most 10,000 rows and `orders` at most 100,000 rows.
- `amount` is always positive. `amount` and `order_date` are never NULL.
- Every `orders.customer_id` references a row in `customers`.
- Orders dated exactly 2024-01-01 or 2024-12-31 are included.
- A customer with several 2024 orders gets one row holding the sum. Customers with no 2024 order 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 finds the three biggest-spending customers across calendar year 2024, counting both boundary dates and breaking ties by customer id. It tests inclusive date range filtering, joining and aggregating with SUM, and deterministic top-N ordering with LIMIT.