Sales Analysis II - Problem

You are given two tables: Product and Sales.

The Product table contains information about products including their unique ID, name, and unit price. The Sales table records all sales transactions with seller ID, product ID, buyer ID, sale date, quantity, and price.

Task: Find all buyers who have bought S8 but have not bought iPhone.

Return the result in any order.

Table Schema

Product
Column Name Type Description
product_id PK int Primary key, unique product identifier
product_name varchar Name of the product (e.g., S8, iPhone)
unit_price int Price per unit of the product
Primary Key: product_id
Sales
Column Name Type Description
seller_id int ID of the seller
product_id int Foreign key referencing Product.product_id
buyer_id int ID of the buyer (never NULL)
sale_date date Date of the sale (never NULL)
quantity int Quantity of products sold
price int Total price of the sale

Input & Output

Example 1 — S8 and iPhone Purchases
Input Tables:
Product
product_id product_name unit_price
1 S8 1000
2 iPhone 1200
3 iPad 800
Sales
seller_id product_id buyer_id sale_date quantity price
1 1 1 2019-01-21 2 2000
1 2 1 2019-02-17 1 800
2 1 2 2019-06-02 1 800
3 3 3 2019-05-13 2 2800
4 1 4 2019-05-13 1 1000
Output:
buyer_id
2
4
💡 Note:

Buyer 1 bought both S8 (product_id=1) and iPhone (product_id=2), so they are excluded. Buyer 2 bought S8 but not iPhone, however they don't exist in this example. Buyer 4 bought only S8 and never bought iPhone, so they are included in the result.

Example 2 — No Valid Buyers
Input Tables:
Product
product_id product_name unit_price
1 S8 1000
2 iPhone 1200
Sales
seller_id product_id buyer_id sale_date quantity price
1 1 1 2019-01-21 1 1000
1 2 1 2019-02-17 1 1200
2 2 2 2019-06-02 1 1200
Output:
buyer_id
💡 Note:

Buyer 1 bought both S8 and iPhone, so they are excluded. Buyer 2 bought only iPhone but not S8, so they don't qualify. No buyers meet the criteria of buying S8 but not iPhone.

Constraints

  • 1 ≤ product_id ≤ 100
  • 1 ≤ seller_id, buyer_id ≤ 1000
  • product_name consists of lowercase letters, digits, and spaces
  • buyer_id is never NULL
  • sale_date is never NULL

Visualization

Tap to expand
Sales Analysis II - Find S8 Buyers (No iPhone) INPUT TABLES Product Table id name price 1 S8 1000 2 G4 800 3 iPhone 1400 Sales Table sale_id prod_id buyer_id qty 1 1 101 2 2 1 102 1 3 3 102 1 4 2 103 2 5 1 103 1 = S8 buyers ALGORITHM STEPS 1 JOIN Tables Product JOIN Sales on id 2 Find S8 Buyers WHERE name = 'S8' S8 Buyers {101, 102, 103} 3 Find iPhone Buyers WHERE name = 'iPhone' iPhone Buyers {102} 4 EXCEPT / NOT IN S8 buyers - iPhone buyers Set Difference S8 iPh FINAL RESULT SELECT DISTINCT s.buyer_id FROM Sales s JOIN Product p ON... WHERE p.name='S8' AND NOT EXISTS... Output buyer_id 101 103 Buyers 101 and 103 bought S8 but never bought iPhone - OK Key Insight: Use NOT EXISTS or NOT IN with a subquery to exclude buyers who purchased iPhone. First find all S8 buyers, then filter out those who also bought iPhone. This is a classic set difference problem in SQL. TutorialsPoint - Sales Analysis II | Optimal Solution
Asked in
Amazon 15 Facebook 12 Microsoft 8
28.5K Views
Medium Frequency
~12 min Avg. Time
890 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