Sales Analysis II - Problem

You're working as a data analyst for a tech retail company and need to identify specific customer purchasing patterns. You have access to two key databases:

Product Table: Contains product information including ID, name, and unit price

Sales Table: Records every transaction with seller, buyer, product, date, quantity, and final price

Your task is to find all buyers who purchased the flagship "S8" smartphone but deliberately avoided buying the "iPhone". This analysis will help the marketing team understand customer brand loyalty and purchasing preferences.

Think of it as finding customers who chose one premium brand over another - valuable insight for competitive analysis!

Return the buyer IDs of these selective customers in any order.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Product table:\n| product_id | product_name | unit_price |\n|------------|--------------|------------|\n| 1 | S8 | 1000 |\n| 2 | iPhone | 1400 |\n| 3 | iPad | 800 |\n\nSales table:\n| seller_id | product_id | buyer_id | sale_date | quantity | price |\n|-----------|------------|----------|-----------|----------|-------|\n| 1 | 1 | 1 | 2019-01-21| 2 | 2000 |\n| 1 | 2 | 1 | 2019-02-17| 1 | 800 |\n| 2 | 1 | 2 | 2019-06-02| 1 | 800 |\n| 3 | 3 | 3 | 2019-05-13| 2 | 2800 |
โ€บ Output: | buyer_id |\n|----------|\n| 2 |
๐Ÿ’ก Note: Buyer 1 purchased both S8 and iPhone, so excluded. Buyer 2 purchased only S8. Buyer 3 purchased only iPad (not S8). Therefore, only buyer 2 meets the criteria.
example_2.sql โ€” Multiple S8 Buyers
$ Input: Product table:\n| product_id | product_name | unit_price |\n|------------|--------------|------------|\n| 1 | S8 | 1000 |\n| 2 | iPhone | 1400 |\n\nSales table:\n| seller_id | product_id | buyer_id | sale_date | quantity | price |\n|-----------|------------|----------|-----------|----------|-------|\n| 1 | 1 | 1 | 2019-01-21| 2 | 2000 |\n| 2 | 1 | 2 | 2019-06-02| 1 | 800 |\n| 3 | 1 | 3 | 2019-07-11| 1 | 800 |
โ€บ Output: | buyer_id |\n|----------|\n| 1 |\n| 2 |\n| 3 |
๐Ÿ’ก Note: All three buyers purchased S8, and none of them purchased iPhone (since there are no iPhone sales), so all qualify.
example_3.sql โ€” No Valid Buyers
$ Input: Product table:\n| product_id | product_name | unit_price |\n|------------|--------------|------------|\n| 1 | S8 | 1000 |\n| 2 | iPhone | 1400 |\n\nSales table:\n| seller_id | product_id | buyer_id | sale_date | quantity | price |\n|-----------|------------|----------|-----------|----------|-------|\n| 1 | 1 | 1 | 2019-01-21| 2 | 2000 |\n| 1 | 2 | 1 | 2019-02-17| 1 | 800 |\n| 2 | 2 | 2 | 2019-06-02| 1 | 800 |
โ€บ Output: Empty result set
๐Ÿ’ก Note: Buyer 1 purchased both S8 and iPhone (excluded). Buyer 2 purchased only iPhone (doesn't meet S8 requirement). No buyers meet the criteria of having S8 but not iPhone.

Constraints

  • 1 โ‰ค product_id โ‰ค 100
  • 1 โ‰ค seller_id, buyer_id โ‰ค 100
  • product_name is case-sensitive
  • Products 'S8' and 'iPhone' are guaranteed to exist in Product table
  • buyer_id is never NULL in Sales table

Visualization

Tap to expand
Sales DatabaseBuyer 1 โ†’ S8Buyer 1 โ†’ iPhoneBuyer 2 โ†’ S8S8 BuyersBuyer 1Buyer 2iPhone BuyersBuyer 1Final ResultS8 Loyal CustomersBuyer 2EXCLUDE๐Ÿ’ก Key Insight: Use NOT IN to exclude buyers with both products
Understanding the Visualization
1
Join Tables
Connect sales records with product information to see what each customer bought
2
Find S8 Customers
Identify all customers who purchased the S8 smartphone
3
Find iPhone Customers
Separately identify customers who purchased iPhone
4
Apply Exclusion
Remove iPhone customers from the S8 customer list
5
Return Loyal S8 Users
The remaining customers show S8 brand loyalty
Key Takeaway
๐ŸŽฏ Key Insight: SQL's NOT IN operator efficiently handles set exclusion, making this a clean one-query solution for finding customers with specific purchase patterns.
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 22
68.4K Views
High Frequency
~15 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