Bird
0
0

Given two tables:

hard📝 Application Q8 of 15
SQL - Set Operations
Given two tables:
sales_2022:
sale_id
10
20
30
40

sales_2023:
sale_id
30
40
50
60

Which query correctly finds sales from 2022 that did not occur in 2023 using EXCEPT?
ASELECT sale_id FROM sales_2022 INTERSECT SELECT sale_id FROM sales_2023;
BSELECT sale_id FROM sales_2023 EXCEPT SELECT sale_id FROM sales_2022;
CSELECT sale_id FROM sales_2022 EXCEPT SELECT sale_id FROM sales_2023;
DSELECT sale_id FROM sales_2022 UNION SELECT sale_id FROM sales_2023;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want sales in 2022 that are not in 2023.
  2. Step 2: Use EXCEPT correctly

    EXCEPT returns rows from the first query that are not in the second. So, selecting from sales_2022 EXCEPT sales_2023 is correct.
  3. Step 3: Analyze options

    SELECT sale_id FROM sales_2022 EXCEPT SELECT sale_id FROM sales_2023; matches the requirement. SELECT sale_id FROM sales_2023 EXCEPT SELECT sale_id FROM sales_2022; reverses the order, giving sales in 2023 not in 2022. SELECT sale_id FROM sales_2022 INTERSECT SELECT sale_id FROM sales_2023; returns common sales. SELECT sale_id FROM sales_2022 UNION SELECT sale_id FROM sales_2023; returns all sales combined.
  4. Final Answer:

    SELECT sale_id FROM sales_2022 EXCEPT SELECT sale_id FROM sales_2023; -> Option C
  5. Quick Check:

    First table EXCEPT second table returns unique rows in first [OK]
Quick Trick: EXCEPT returns rows unique to first query's result [OK]
Common Mistakes:
MISTAKES
  • Swapping the order of tables 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