Bird
0
0

You have two customer lists:

hard📝 Application Q15 of 15
SQL - Set Operations
You have two customer lists:
List A: customers who bought product X
List B: customers who bought product Y
How do you find customers who bought either product X or Y but not both using set operations?
ASELECT * FROM A UNION SELECT * FROM B
B(SELECT * FROM A EXCEPT SELECT * FROM B) UNION (SELECT * FROM B EXCEPT SELECT * FROM A)
CSELECT * FROM A INTERSECT SELECT * FROM B
D(SELECT * FROM A EXCEPT SELECT * FROM B) UNION (SELECT * FROM A INTERSECT SELECT * FROM B)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the problem

    We want customers who bought product X or Y but not both (exclusive customers).
  2. Step 2: Use EXCEPT and UNION to find exclusive customers

    Find customers in A but not in B, and customers in B but not in A, then combine them with UNION.
  3. Step 3: Check other options

    SELECT * FROM A UNION SELECT * FROM B gives all customers who bought either product (including both). SELECT * FROM A INTERSECT SELECT * FROM B gives only those who bought both. (SELECT * FROM A EXCEPT SELECT * FROM B) UNION (SELECT * FROM A INTERSECT SELECT * FROM B) gives all customers who bought product X.
  4. Final Answer:

    (SELECT * FROM A EXCEPT SELECT * FROM B) UNION (SELECT * FROM B EXCEPT SELECT * FROM A) -> Option B
  5. Quick Check:

    Exclusive customers = (A EXCEPT B) UNION (B EXCEPT A) [OK]
Quick Trick: Use EXCEPT both ways, then UNION results [OK]
Common Mistakes:
MISTAKES
  • Using UNION alone includes both customers
  • Using INTERSECT returns only common customers
  • Confusing EXCEPT with INTERSECT

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes