Bird
0
0

You want to list all customers who bought either product A or product B. Tables:

hard📝 Application Q9 of 15
SQL - Set Operations
You want to list all customers who bought either product A or product B. Tables:
ProductA_Customers: customer_id
ProductB_Customers: customer_id
Which query correctly lists all unique customers who bought either product?
ASELECT customer_id FROM ProductA_Customers UNION SELECT customer_id FROM ProductB_Customers;
BSELECT customer_id FROM ProductA_Customers UNION ALL SELECT customer_id FROM ProductB_Customers;
CSELECT DISTINCT customer_id FROM ProductA_Customers JOIN ProductB_Customers;
DSELECT customer_id FROM ProductA_Customers INTERSECT SELECT customer_id FROM ProductB_Customers;
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement for unique customers

    We want all customers who bought either product, no duplicates.
  2. Step 2: Use UNION to combine and remove duplicates

    UNION combines both lists and removes duplicates.
  3. Step 3: Check other options

    UNION ALL keeps duplicates; JOIN lists only customers who bought both; INTERSECT lists common customers only.
  4. Final Answer:

    SELECT customer_id FROM ProductA_Customers UNION SELECT customer_id FROM ProductB_Customers; -> Option A
  5. Quick Check:

    UNION for unique combined list = B [OK]
Quick Trick: Use UNION to get unique combined customer list [OK]
Common Mistakes:
MISTAKES
  • Using UNION ALL causing duplicates
  • Using JOIN or INTERSECT instead of UNION
  • Not removing duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes