Latest Purchase Summary
Beginner Mode
Objective
Write a SQL query to retrieve each user's most recent transaction date along with the number of products they bought on that date.
Table Schema:
- transactions
| Column | Type | Description |
|---|---|---|
| transaction_id | INTEGER | Unique identifier for each transaction |
| user_id | INTEGER | ID of the user |
| transaction_date | DATE | Date of the transaction |
| product_id | INTEGER | ID of the product purchased |
| spend | DECIMAL | Amount spent on the transaction |
Task Requirements:
- For each user, identify their most recent transaction date
- Count the number of products purchased by that user on their most recent date
- A user may have bought multiple products on the same day, each as a separate transaction
- Sort the results chronologically by transaction date in ascending order
Output columns: user_id, transaction_date, num_products
Examples
Example 1:
Output:
Input:
| transactions | ||||
|---|---|---|---|---|
| product_id | spend | transaction_date | transaction_id | user_id |
| 101 | 29.99 | 2023-01-05 | 1 | 1 |
| 102 | 45.5 | 2023-01-05 | 2 | 1 |
| 103 | 12.75 | 2023-01-03 | 3 | 1 |
| 201 | 67.2 | 2023-01-08 | 4 | 2 |
| 202 | 33.4 | 2023-01-06 | 5 | 2 |
| 203 | 18.9 | 2023-01-06 | 6 | 2 |
| 301 | 54 | 2023-01-02 | 7 | 3 |
| num_products | transaction_date | user_id |
|---|---|---|
| 1 | 2023-01-02 | 3 |
| 2 | 2023-01-05 | 1 |
| 1 | 2023-01-08 | 2 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Shopify