Market Analysis: First Item

ASKED IN INTERVIEW
4pts
Amazon

Problem Statement

Amazon marketing wants to know if users' early shopping behavior aligns with their stated brand preferences.

Write an SQL query to find, for each user, whether the brand of the second item (by date) they purchased is the same as their favorite brand.

Rules:

  • If a user has purchased less than two items, the answer should be "no".
  • Output columns: seller_id, 2nd_item_fav_brand.
  • 2nd_item_fav_brand should be "yes" if the brands match, and "no" otherwise.
  • Results must be sorted by seller_id in ascending order.
Tests your understanding of
Window Functions, Ranking and Joins

Input Tables

users
user_id(INTEGER)join_date(DATE)favorite_brand(VARCHAR)
12025-01-01Samsung
22025-02-09Apple
32025-01-19LG
42025-05-21HP
orders
order_id(INTEGER)order_date(DATE)item_id(INTEGER)seller_id(INTEGER)
12025-08-011011
22025-08-021021
32025-08-031032
42025-08-041042
52025-08-051013
items
item_id(INTEGER)item_brand(VARCHAR)
101Samsung
102Samsung
103Apple
104Sony

Expected Output

seller_id(INTEGER)2nd_item_fav_brand(VARCHAR)
1yes
2no
3no
4no

Tags

MediumASKED IN INTERVIEWWindow FunctionsRankingJoins
20-25 min
55%

Hints