Bird
0
0

Given the tables and query below, what will be the output?

medium📝 query result Q13 of 15
SQL - Set Operations

Given the tables and query below, what will be the output?

Table X:
id | value
1 | 'A'
2 | 'B'

Table Y:
code | val
2 | 'B'
3 | 'C'

Query:
SELECT id, value FROM X
UNION
SELECT code, val FROM Y;
ASyntax error due to column name mismatch
B[ (1, 'A'), (2, 'B'), (3, 'C') ]
C[ (1, 'A'), (3, 'C') ]
D[ (1, 'A'), (2, 'B'), (2, 'B'), (3, 'C') ]
Step-by-Step Solution
Solution:
  1. Step 1: Check column counts and types

    Both queries select 2 columns with compatible types (INT and VARCHAR). Column names differ but that is allowed.
  2. Step 2: Understand UNION behavior

    UNION removes duplicates. Rows (2, 'B') appear in both tables, so only one copy appears in result.
  3. Final Answer:

    [ (1, 'A'), (2, 'B'), (3, 'C') ] -> Option B
  4. Quick Check:

    UNION removes duplicates = D [OK]
Quick Trick: UNION removes duplicates, column names don't matter [OK]
Common Mistakes:
MISTAKES
  • Expecting duplicate rows in UNION result
  • Thinking column names must match
  • Confusing UNION with UNION ALL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes