Bird
0
0

You have two tables:

hard📝 Application Q15 of 15
SQL - Set Operations
You have two tables:
orders_2023 and orders_2024, both with columns order_id and customer_id.

You want to find all orders from 2023 that were NOT repeated in 2024.

Which query correctly finds these unique 2023 orders?
ASELECT order_id, customer_id FROM orders_2023 INTERSECT SELECT order_id, customer_id FROM orders_2024;
BSELECT order_id, customer_id FROM orders_2024 EXCEPT SELECT order_id, customer_id FROM orders_2023;
CSELECT order_id, customer_id FROM orders_2023 UNION SELECT order_id, customer_id FROM orders_2024;
DSELECT order_id, customer_id FROM orders_2023 EXCEPT SELECT order_id, customer_id FROM orders_2024;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want orders in 2023 that do NOT appear in 2024.
  2. Step 2: Choose correct set operation

    EXCEPT returns rows in first query not in second, so orders_2023 EXCEPT orders_2024 fits.
  3. Step 3: Check other options

    SELECT order_id, customer_id FROM orders_2024 EXCEPT SELECT order_id, customer_id FROM orders_2023; reverses order, giving 2024 unique orders. SELECT order_id, customer_id FROM orders_2023 INTERSECT SELECT order_id, customer_id FROM orders_2024; returns common orders. SELECT order_id, customer_id FROM orders_2023 UNION SELECT order_id, customer_id FROM orders_2024; combines all orders.
  4. Final Answer:

    SELECT order_id, customer_id FROM orders_2023 EXCEPT SELECT order_id, customer_id FROM orders_2024; -> Option D
  5. Quick Check:

    2023 orders minus 2024 orders = unique 2023 orders [OK]
Quick Trick: Use EXCEPT with 2023 first, 2024 second to find unique 2023 orders [OK]
Common Mistakes:
MISTAKES
  • Swapping table order in EXCEPT
  • Using INTERSECT instead of EXCEPT
  • Using UNION which combines all rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes