Bird
0
0

Given two tables:

medium📝 query result Q4 of 15
SQL - Set Operations
Given two tables:
table1 with values (1, 'Anna'), (2, 'Ben')
table2 with values (2, 'Ben'), (3, 'Cara')
What is the result of:
SELECT id, name FROM table1 UNION SELECT id, name FROM table2 ORDER BY id;?
A(2, 'Ben'), (3, 'Cara')
B(1, 'Anna'), (2, 'Ben'), (3, 'Cara')
C(1, 'Anna'), (2, 'Ben'), (2, 'Ben'), (3, 'Cara')
D(3, 'Cara'), (2, 'Ben'), (1, 'Anna')
Step-by-Step Solution
Solution:
  1. Step 1: Apply UNION operation

    UNION removes duplicates, so (2, 'Ben') appears once.

  2. Step 2: Apply ORDER BY on combined result

    Ordering by id gives rows sorted as (1, 'Anna'), (2, 'Ben'), (3, 'Cara').

  3. Final Answer:

    (1, 'Anna'), (2, 'Ben'), (3, 'Cara') -> Option B
  4. Quick Check:

    UNION removes duplicates, ORDER BY sorts final rows [OK]
Quick Trick: UNION removes duplicates before ORDER BY sorts [OK]
Common Mistakes:
MISTAKES
  • Expecting duplicate rows in UNION
  • Ignoring ORDER BY effect
  • Confusing UNION with UNION ALL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes