Product Sales Analysis IV

ASKED IN INTERVIEW
4pts
Walmart

Problem Statement

Walmart wants to understand customer loyalty toward specific products. Write an SQL query that finds, for each user, the product_id that they spent the most money on.

Total spending for a product is calculated as the sum of (unit_price * quantity) across all sales of that product for that user.

Rules:

  • If there is a tie for the most money spent by a user on multiple products, return all such products.
  • Output columns: user_id, product_id.
  • Results must be sorted by user_id in ascending order, then product_id in ascending order.
Tests your understanding of
Window Functions, Ranking and Aggregation

Input Tables

sales
sale_id(INTEGER)product_id(INTEGER)user_id(INTEGER)quantity(INTEGER)
11012
22011
31025
43021
product
product_id(INTEGER)price(INTEGER)
10100
20500
301000
store_locations
store_id(INTEGER)city(VARCHAR)
501Bentonville
502Dallas

Expected Output

user_id(INTEGER)product_id(INTEGER)
120
230

Tags

MediumASKED IN INTERVIEWWindow FunctionsRankingAggregation
20-25 min
58%

Hints