Market Analysis II - Problem
Market Analysis II - E-commerce Analytics

You're working as a data analyst for a major e-commerce platform and need to analyze seller behavior patterns. The platform has three main data sources:

Users Table: Contains user information including their user_id, join_date, and most importantly, their favorite_brand preference.

Orders Table: Records all transactions with order_id, order_date, item_id, buyer_id, and seller_id.

Items Table: Contains item details including item_id and item_brand.

Your Mission: For each user on the platform, determine whether the second item they sold (chronologically by date) matches their personal favorite brand. This analysis will help understand if sellers naturally gravitate toward selling items from brands they personally prefer.

Special Rules:
• If a user sold fewer than 2 items total, report "no"
• No seller sells more than one item per day (guaranteed)
• Focus only on the second item sold chronologically

The result should show each user and whether their second sale aligned with their brand preference.

Input & Output

example_1.sql — Basic Case
$ Input: Users: [(1, '2019-01-01', 'Lenovo'), (2, '2019-02-09', 'Samsung'), (3, '2019-01-19', 'LG')] Orders: [(1, '2019-08-01', 1, 1, 1), (2, '2019-08-02', 2, 1, 2), (3, '2019-08-03', 3, 2, 3)] Items: [(1, 'Samsung'), (2, 'Lenovo'), (3, 'LG')]
Output: [(1, 'no'), (2, 'no'), (3, 'no')]
💡 Note: User 1 sold Samsung item first, then Lenovo (matches favorite). User 2 and 3 each sold only 1 item, so result is 'no'.
example_2.sql — Multiple Sales
$ Input: Users: [(1, '2019-01-01', 'Apple'), (2, '2019-02-09', 'Samsung')] Orders: [(1, '2019-08-01', 1, 1, 1), (2, '2019-08-02', 2, 1, 2), (3, '2019-08-03', 3, 2, 1), (4, '2019-08-04', 4, 2, 2)] Items: [(1, 'Samsung'), (2, 'Apple'), (3, 'Apple'), (4, 'Samsung')]
Output: [(1, 'yes'), (2, 'yes')]
💡 Note: User 1's second item is Apple (matches favorite). User 2's second item is Samsung (matches favorite).
example_3.sql — Edge Case
$ Input: Users: [(1, '2019-01-01', 'Nike')] Orders: [(1, '2019-08-01', 1, 1, 1)] Items: [(1, 'Nike')]
Output: [(1, 'no')]
💡 Note: User 1 sold only one item, so the result is 'no' regardless of brand match.

Constraints

  • 1 ≤ Users.user_id ≤ 104
  • 1 ≤ Orders.order_id ≤ 105
  • 1 ≤ Items.item_id ≤ 103
  • No seller sells more than one item on the same date
  • All foreign key relationships are maintained
  • Dates are in YYYY-MM-DD format

Visualization

Tap to expand
Seller DatabaseUser preferences& sale historyRanking EngineROW_NUMBER()by dateBrand MatcherCompare 2nd itemwith favoriteExample Flow:Seller John (favorite: Apple)• 2023-01-01: Sold Samsung Phone → rank 1• 2023-01-05: Sold Apple Watch → rank 2 ✓• 2023-01-10: Sold Dell Laptop → rank 3Result: YES (Apple Watch matches favorite Apple)👤#2Market Analysis II - Brand Preference Detection
Understanding the Visualization
1
Collect Sales Data
Gather all sales records with dates and item brands
2
Rank by Seller
For each seller, order their sales chronologically
3
Find Second Sale
Identify the second item sold by each seller
4
Check Brand Match
Compare second item's brand with seller's favorite brand
Key Takeaway
🎯 Key Insight: Window functions like ROW_NUMBER() allow us to efficiently rank and filter data within groups, avoiding expensive nested subqueries and providing optimal performance for analytical queries.
Asked in
Amazon 45 Meta 38 Google 32 Microsoft 28
38.5K Views
High Frequency
~25 min Avg. Time
1.4K 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