Latest Purchase Summary
Shopify ☯️ Medium SQL
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:

Input:

transactions
product_idspendtransaction_datetransaction_iduser_id
10129.992023-01-0511
10245.52023-01-0521
10312.752023-01-0331
20167.22023-01-0842
20233.42023-01-0652
20318.92023-01-0662
301542023-01-0273

Output:
num_productstransaction_dateuser_id
12023-01-023
22023-01-051
12023-01-082
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →