Bird
0
0

You have two tables:

hard📝 Application Q15 of 15
PostgreSQL - Set Operations and Advanced Queries
You have two tables:
employees with columns id, name
contractors with columns id, name
You want to find people who are either employees or contractors but not both.
Which query correctly returns this list?
ASELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees;
BSELECT id, name FROM employees INTERSECT SELECT id, name FROM contractors;
CSELECT id, name FROM employees UNION SELECT id, name FROM contractors;
DSELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want people in employees or contractors but not in both (symmetric difference).
  2. Step 2: Use EXCEPT and UNION to find symmetric difference

    EXCEPT finds those in one table but not the other; UNION combines both exclusive sets.
  3. Step 3: Analyze options

    SELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees; correctly combines EXCEPT in both directions with UNION to get symmetric difference.
  4. Final Answer:

    SELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees; -> Option A
  5. Quick Check:

    Symmetric difference = EXCEPT + UNION [OK]
Quick Trick: Symmetric difference = (A EXCEPT B) UNION (B EXCEPT A) [OK]
Common Mistakes:
  • Using only INTERSECT or UNION
  • Using EXCEPT one way only
  • Confusing symmetric difference with intersection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes