Top Spenders Per Region With No Recent Activity
Company: McKinsey
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
You have a `users` table and an `orders` table. Using CTEs and window functions, find the top 3 users by total spend in each region among users with no purchase in the last 30 days relative to the reference date `2024-06-01`. Return `user_id`, `region`, `total_spend` and `region_rank`.
```hint Where to test inactivity
Decide whether a user is inactive by looking at all of their orders together, after grouping. Filtering order rows by date before grouping changes which users survive.
```
### Input Table(s)
`users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier |
| region | VARCHAR(50) | Region the user belongs to, never NULL |
Primary key: `user_id`.
`orders`
| Column | Type | Meaning |
|---|---|---|
| order_id | INT | Order identifier |
| user_id | INT | User who placed the order |
| amount | DECIMAL(10,2) | Order value, positive, never NULL |
| order_date | DATE | Date of the order, never NULL |
Primary key: `order_id`. Foreign key: `orders.user_id` references `users.user_id`.
### Output Contract
Return one row per selected user with these columns, in this order:
| Column | Meaning |
|---|---|
| user_id | The user |
| region | The user's region |
| total_spend | Sum of `amount` over all of the user's orders, exact, no rounding |
| region_rank | `RANK()` partitioned by `region`, ordered by `total_spend` descending then `user_id` ascending |
- Exclude users with any order in the last 30 days, and users with no orders.
- Keep only the top 3 per region (`region_rank` of 1 to 3).
- Sort by `region`, `region_rank`, `user_id`, all ascending.
### Example
`users`
| user_id | region |
|---|---|
| 1 | East |
| 2 | East |
| 3 | East |
| 4 | East |
| 5 | East |
| 6 | West |
| 7 | West |
| 8 | West |
`orders`
| order_id | user_id | amount | order_date |
|---|---|---|---|
| 101 | 1 | 120.00 | 2024-03-10 |
| 102 | 1 | 80.00 | 2024-04-15 |
| 103 | 2 | 200.00 | 2024-01-05 |
| 104 | 3 | 300.00 | 2024-02-20 |
| 105 | 3 | 50.00 | 2024-05-20 |
| 106 | 4 | 200.00 | 2023-12-01 |
| 107 | 5 | 250.00 | 2024-05-01 |
| 108 | 6 | 90.00 | 2024-05-02 |
| 109 | 7 | 60.00 | 2023-11-11 |
Expected output
| user_id | region | total_spend | region_rank |
|---|---|---|---|
| 5 | East | 250.00 | 1 |
| 1 | East | 200.00 | 2 |
| 2 | East | 200.00 | 3 |
| 7 | West | 60.00 | 1 |
The 30-day window starts on 2024-05-02. User 3 has the largest total in East (350.00) but ordered on 2024-05-20, so it is excluded. User 6 ordered on 2024-05-02, the first day of the window, so it is excluded too. User 5's only order is on 2024-05-01, one day before the window, so user 5 counts as inactive. Users 1, 2 and 4 each spent 200.00; the `user_id` tiebreak gives them ranks 2, 3 and 4, and user 4 falls outside the top 3. User 8 has no orders.
### Constraints and Clarifications
- 1 to 10,000 users and 0 to 100,000 orders.
- `amount` is a positive decimal, and `order_date` is between 2020-01-01 and 2024-06-01.
- An order is recent when its `order_date` is on or after 2024-05-02, the reference date minus 30 days. One recent order excludes the user, whatever their older orders look like.
- `total_spend` is the sum over all of the user's orders. For an inactive user every order is older than the window, so this is also their spend before 2024-05-02.
- No column involved can be NULL.
- The rank ordering ends in `user_id`, which is unique, so no two users in a region share a rank and each region returns at most 3 rows. A region with no inactive users does not appear.
Overview: Write a SQL query with CTEs and window functions that ranks the top three spenders in each region, counting only users with no orders in the 30 days before a reference date. It tests grouped aggregation, inactivity checks on the latest order date, RANK with a tiebreak, and date boundary handling.