Market Analysis III - Problem
Market Analysis III is a fascinating SQL problem that challenges you to analyze e-commerce marketplace data across multiple dimensions.

You're given three interconnected tables representing an online marketplace ecosystem:

πŸ“Š Users Table: Contains seller information including their seller_id, join_date, and favorite_brand
πŸ“¦ Items Table: Maps each item_id to its corresponding item_brand
πŸ›’ Orders Table: Records all transactions with order_id, order_date, item_id, and seller_id

Your mission: Find the top-performing sellers who have demonstrated the most diverse selling behavior by identifying those who sold the highest number of unique items from brands different from their personal favorite brand.

This problem tests your ability to:
β€’ Join multiple tables with foreign key relationships
β€’ Filter data based on complex conditions
β€’ Count distinct values with grouping
β€’ Handle ties in ranking scenarios

If multiple sellers tie for the highest count, return all of them sorted by seller_id in ascending order.

Input & Output

example_1.sql β€” Basic Case
$ Input: Users: [(1, '2019-01-01', 'Lenovo'), (2, '2019-02-09', 'Samsung'), (3, '2019-01-19', 'LG')] Items: [(1, 'Samsung'), (2, 'Lenovo'), (3, 'LG'), (4, 'HP')] Orders: [(1, '2019-05-21', 1, 1), (2, '2019-05-13', 2, 1), (3, '2019-06-15', 1, 2), (4, '2019-05-13', 4, 2), (5, '2019-07-11', 3, 3)]
β€Ί Output: [1, 2]
πŸ’‘ Note: Seller 1 sold items from Samsung and Lenovo brands, but their favorite is Lenovo, so they sold 1 unique non-favorite item (Samsung). Seller 2 sold Samsung and HP items, but their favorite is Samsung, so they sold 1 unique non-favorite item (HP). Seller 3 only sold LG items, which matches their favorite brand, so 0 unique non-favorite items. Sellers 1 and 2 tie with 1 unique non-favorite item each.
example_2.sql β€” Clear Winner
$ Input: Users: [(1, '2020-01-01', 'Apple'), (2, '2020-02-01', 'Samsung')] Items: [(1, 'Samsung'), (2, 'Apple'), (3, 'LG'), (4, 'HP'), (5, 'Dell')] Orders: [(1, '2020-03-01', 1, 1), (2, '2020-03-02', 3, 1), (3, '2020-03-03', 4, 1), (4, '2020-03-04', 2, 2)]
β€Ί Output: [1]
πŸ’‘ Note: Seller 1 (favorite: Apple) sold Samsung, LG, and HP items = 3 unique non-favorite items. Seller 2 (favorite: Samsung) sold only Apple items = 1 unique non-favorite item. Seller 1 wins with 3.
example_3.sql β€” All Sellers Sell Only Favorites
$ Input: Users: [(1, '2021-01-01', 'Apple'), (2, '2021-02-01', 'Samsung')] Items: [(1, 'Apple'), (2, 'Samsung')] Orders: [(1, '2021-03-01', 1, 1), (2, '2021-03-02', 2, 2)]
β€Ί Output: [1, 2]
πŸ’‘ Note: Both sellers only sold items matching their favorite brands. Seller 1 sold Apple (favorite: Apple) = 0 non-favorite items. Seller 2 sold Samsung (favorite: Samsung) = 0 non-favorite items. They tie with 0 each.

Constraints

  • 1 ≀ Number of users, items, orders ≀ 104
  • All seller_id, item_id, and order_id are positive integers
  • seller_id in Orders table references Users table
  • item_id in Orders table references Items table
  • Brand names are non-empty strings with maximum length 50

Visualization

Tap to expand
Users Tableseller_idfavorite_brandOrders Tableseller_iditem_idItems Tableitem_iditem_brandJOINJOINJoined DataAll relationships combinedFilter Conditionitem_brand β‰  favorite_brandGROUP BY seller_idCOUNT DISTINCT itemsFind MAX CountIdentify winnersπŸ† Final ResultTop sellers (sorted by ID)πŸ’‘ Single SQL query with JOINs and conditional aggregation
Understanding the Visualization
1
JOIN Operation
Combine Users (seller preferences), Orders (transactions), and Items (product catalog) into a unified view
2
Filter Condition
Keep only records where item_brand β‰  favorite_brand (sellers selling outside their preference)
3
Group and Count
GROUP BY seller_id and COUNT DISTINCT item_id to find unique non-favorite items per seller
4
Find Maximum
Identify the highest count among all sellers
5
Return Winners
Select all sellers who achieved the maximum count, sorted by seller_id
Key Takeaway
🎯 Key Insight: Use JOINs to combine tables first, then conditional aggregation with CASE to count only items that don't match seller's favorite brand. This eliminates the need for multiple passes through the data.
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Apple 22
43.7K Views
High Frequency
~25 min Avg. Time
1.8K 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