User Purchase Hist.

ASKED IN INTERVIEW
4pts
Walmart

Problem Statement

Walmart wants to analyze recency of shopping behavior. You need to identify how many users were active on their latest recorded purchase date in the system.

Write an SQL query to find the count of users who made at least one purchase on the latest date that appears for them in the purchase history.

Rules:

  • The latest date is unique to each user.
  • Only return the final count of these users.
  • Output column: user_count.
  • Result should be an integer.
Tests your understanding of
Dates, Filtering, Aggregation and Subqueries

Input Tables

users
user_id(INTEGER)user_name(VARCHAR)
1Alice
2Bob
3Charlie
4David
products
product_id(INTEGER)product_name(VARCHAR)
501Groceries
502Electronics
purchases
purchase_id(INTEGER)user_id(INTEGER)product_id(INTEGER)purchase_date(TIMESTAMP)
115012023-01-01 10:00:00
215022023-01-05 12:00:00
325012023-01-02 09:00:00
425022023-01-02 15:00:00
535012023-01-10 08:00:00
645022023-01-01 10:00:00

Expected Output

user_count(INTEGER)
4

Tags

MediumASKED IN INTERVIEWDatesFilteringAggregationSubqueries
15-20 min
52%

Hints