Market Analysis II - Problem

You have three tables in an online marketplace database:

Users table: Contains user information including their user_id, join_date, and favorite_brand.

Orders table: Contains order information with order_id, order_date, item_id, buyer_id, and seller_id.

Items table: Contains item information with item_id and item_brand.

Task: For each user, determine whether the brand of the second item (chronologically by date) they sold matches their favorite brand. If a user sold fewer than two items, report 'no' for that user.

Note: It is guaranteed that no seller sells more than one item per day, so there are no tie-breaking issues for ordering by date.

Table Schema

Users
Column Name Type Description
user_id PK int Primary key, unique user identifier
join_date date Date when user joined the platform
favorite_brand varchar User's favorite brand preference
Primary Key: user_id
Orders
Column Name Type Description
order_id PK int Primary key, unique order identifier
order_date date Date when order was placed
item_id int Foreign key referencing Items table
buyer_id int Foreign key referencing Users table
seller_id int Foreign key referencing Users table
Primary Key: order_id
Items
Column Name Type Description
item_id PK int Primary key, unique item identifier
item_brand varchar Brand name of the item
Primary Key: item_id

Input & Output

Example 1 — Mixed Results
Input Tables:
Users
user_id join_date favorite_brand
1 2019-01-01 Lenovo
2 2019-02-09 Samsung
3 2019-01-19 LG
Orders
order_id order_date item_id buyer_id seller_id
1 2019-08-01 4 1 2
2 2019-08-02 2 1 3
3 2019-08-03 3 2 3
4 2019-08-04 1 4 2
5 2019-08-04 1 3 4
Items
item_id item_brand
1 Samsung
2 Lenovo
3 LG
4 HP
Output:
seller_id 2nd_item_fav_brand
1 no
2 yes
3 no
4 no
💡 Note:

User 1 has no sales, so result is 'no'. User 2 sold HP first (2019-08-01) and Samsung second (2019-08-04) - Samsung matches their favorite brand, so 'yes'. User 3 sold Lenovo first (2019-08-02) and LG second (2019-08-03) - LG matches their favorite brand, but wait... User 3's favorite is LG and second item is LG, so should be 'yes'. User 4 only sold one item, so 'no'.

Example 2 — Single Sale Users
Input Tables:
Users
user_id join_date favorite_brand
1 2019-01-01 Apple
2 2019-02-09 Samsung
Orders
order_id order_date item_id buyer_id seller_id
1 2019-08-01 1 2 1
Items
item_id item_brand
1 Apple
Output:
seller_id 2nd_item_fav_brand
1 no
2 no
💡 Note:

User 1 sold only one item (Apple), so even though it matches their favorite brand, we need the second item - result is 'no'. User 2 made no sales, so result is 'no'.

Constraints

  • 1 ≤ Users.user_id ≤ 1000
  • join_date and order_date are valid dates
  • No seller sells more than one item on the same day
  • All foreign key references are valid

Visualization

Tap to expand
Market Analysis II: Finding Second Item MatchUsers Tableuser_idfavorite_brand1Lenovo2SamsungSales Data (Ranked)seller_idbrandrank2Samsung2CompareBrandsFinal Resultseller_id2nd_item_fav_brand1no2yesUsers with fewer than 2 sales get 'no' resultSecond item brand compared with user's favorite brand
Understanding the Visualization
1
Join Tables
Combine Orders and Items to get brands
2
Rank Sales
Use ROW_NUMBER() to order sales by date
3
Filter Second
Keep only rank = 2 items
4
Compare Brands
Check if second item brand matches favorite
Key Takeaway
🎯 Key Insight: Use ROW_NUMBER() window function to identify chronologically ordered sales and handle users with insufficient sales data
Asked in
Amazon 15 Facebook 8 Microsoft 5
28.5K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen