Bird
0
0

You have two tables:

hard📝 Application Q15 of 15
SQL - Set Operations
You have two tables:
Sales2023(product, amount)
Apple, 100
Banana, 150

Sales2024(product, amount)
Banana, 200
Cherry, 300

Write a query using UNION to list all unique products sold in both years, sorted alphabetically.
ASELECT product, amount FROM Sales2023 UNION SELECT product, amount FROM Sales2024 ORDER BY amount;
BSELECT product FROM Sales2023 UNION ALL SELECT product FROM Sales2024 ORDER BY product;
CSELECT product FROM Sales2023 UNION SELECT product FROM Sales2024 ORDER BY product;
DSELECT product FROM Sales2023 JOIN Sales2024 ON product ORDER BY product;
Step-by-Step Solution
Solution:
  1. Step 1: Select product column from both tables

    We want unique products, so select only the product column from both tables.
  2. Step 2: Use UNION to combine and remove duplicates

    UNION combines both lists and removes duplicates, giving unique products.
  3. Step 3: Sort results alphabetically

    ORDER BY product sorts the final list alphabetically.
  4. Final Answer:

    SELECT product FROM Sales2023 UNION SELECT product FROM Sales2024 ORDER BY product; -> Option C
  5. Quick Check:

    UNION + ORDER BY product = B [OK]
Quick Trick: Use UNION to remove duplicates, ORDER BY to sort [OK]
Common Mistakes:
MISTAKES
  • Using UNION ALL which keeps duplicates
  • Selecting amount column when only product needed
  • Using JOIN instead of UNION

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes