Total Spending per Item Among Users Registered in 2022
Company: Costco
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find the total amount spent on each item, counting only purchases by users with `registration_year = 2022`. Return `item_name` and `total_amount`, ordered by `total_amount` descending. If no 2022 users exist, the result is empty.
```hint Where each fact lives
The year you filter on is in one table and the amounts you add up are in the other. Check whether connecting them can ever count a purchase twice.
```
### Input Table(s)
Table `users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Unique id of the user. Primary key. |
| registration_year | INT | Year the user registered. Nullable. |
| user_name | VARCHAR(100) | Display name. Nullable. |
Primary key: `user_id`.
Table `purchases`
| Column | Type | Meaning |
|---|---|---|
| purchase_id | INT | Unique id of the purchase. Primary key. |
| user_id | INT | The buyer, matching `users.user_id`. Nullable. |
| item_name | VARCHAR(100) | Name of the item bought. |
| amount | DECIMAL(10,2) | Amount paid for the purchase. |
Primary key: `purchase_id`.
### Output Contract
- Columns `item_name`, `total_amount`, in that order.
- One row per item that has at least one purchase by a 2022 user.
- `total_amount` is the exact sum of `amount` over those purchases, with no rounding.
- Rows sorted by `total_amount` descending, then `item_name` ascending for equal totals. `item_name` is unique per row, so the order is fully determined.
### Example
`users`
| user_id | registration_year | user_name |
|---|---|---|
| 1 | 2022 | Ana |
| 2 | 2021 | Ben |
| 3 | 2022 | Chloe |
| 4 | 2023 | Dev |
`purchases`
| purchase_id | user_id | item_name | amount |
|---|---|---|---|
| 1 | 1 | Paper towels | 24.00 |
| 2 | 2 | Paper towels | 22.99 |
| 3 | 1 | Rotisserie chicken | 4.99 |
| 4 | 3 | Rotisserie chicken | 4.99 |
| 5 | 3 | Olive oil | 15.50 |
| 6 | 4 | Olive oil | 15.50 |
| 7 | 2 | Coffee beans | 18.00 |
| 8 | 3 | Almonds | 9.98 |
Expected result
| item_name | total_amount |
|---|---|
| Paper towels | 24.00 |
| Olive oil | 15.50 |
| Almonds | 9.98 |
| Rotisserie chicken | 9.98 |
Users 1 and 3 registered in 2022, so only purchases 1, 3, 4, 5 and 8 count. Rotisserie chicken is 4.99 + 4.99 = 9.98, which ties with Almonds, and the tie goes to the alphabetically earlier name. Coffee beans was bought only by a 2021 user, so it does not appear.
### Constraints and Clarifications
- `registration_year` is a 4-digit integer when present. A NULL year is not 2022.
- `amount` is a non-negative decimal. Assume every purchase has an `item_name` and an `amount`.
- A purchase whose `user_id` is NULL or has no row in `users` does not belong to a 2022 user and is excluded.
- Purchases of the same `item_name` by different 2022 users add into one total.
- Items with no purchases by 2022 users do not appear, not even with a zero total. If there are no 2022 users, or none of them bought anything, the result is empty.
Overview: A SQL problem with a users table and a purchases table: total the amount spent on each item, counting only purchases by users who registered in 2022, and sort the items by that total. It tests filtering through a join, grouping with SUM, and ordering with a tie-break.