Bird
0
0

You have two tables:

hard📝 Application Q8 of 15
SQL - Set Operations
You have two tables:
Orders(order_id, customer_id)
Returns(order_id, customer_id)
Write a query to list all order_ids from both tables including duplicates using UNION ALL. Which query is correct?
ASELECT order_id FROM Orders JOIN Returns ON order_id UNION ALL;
BSELECT order_id FROM Orders UNION SELECT order_id FROM Returns;
CSELECT order_id FROM Orders UNION ALL SELECT order_id FROM Returns;
DSELECT order_id FROM Orders UNION ALL SELECT customer_id FROM Returns;
Step-by-Step Solution
Solution:
  1. Step 1: Identify columns to select

    We want order_id from both tables.
  2. Step 2: Use UNION ALL to combine including duplicates

    SELECT order_id FROM Orders UNION ALL SELECT order_id FROM Returns; correctly selects order_id from both tables and combines with UNION ALL.
  3. Step 3: Check other options for errors

    A has invalid JOIN syntax, B uses UNION (removes duplicates), D mismatches columns (customer_id).
  4. Final Answer:

    SELECT order_id FROM Orders UNION ALL SELECT order_id FROM Returns; -> Option C
  5. Quick Check:

    Correct UNION ALL query = SELECT order_id FROM Orders UNION ALL SELECT order_id FROM Returns; [OK]
Quick Trick: UNION ALL combines same columns from both tables including duplicates [OK]
Common Mistakes:
MISTAKES
  • Using UNION instead of UNION ALL
  • Mismatching columns in SELECTs
  • Incorrect JOIN syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes