Bird
0
0

Given two tables:

medium📝 query result Q4 of 15
PostgreSQL - Set Operations and Advanced Queries
Given two tables:
Table1:
id | name
1 | 'Alice'
2 | 'Bob'

Table2:
id | name
2 | 'Bob'
3 | 'Charlie'

What is the result of this query?
SELECT id, name FROM Table1 UNION SELECT id, name FROM Table2;
ARows: (1, 'Alice'), (2, 'Bob'), (2, 'Bob'), (3, 'Charlie')
BRows: (1, 'Alice'), (2, 'Bob'), (3, 'Charlie')
CRows: (2, 'Bob'), (3, 'Charlie')
DRows: (1, 'Alice'), (3, 'Charlie')
Step-by-Step Solution
Solution:
  1. Step 1: Understand UNION removes duplicates

    The query combines rows from both tables but removes duplicate rows like (2, 'Bob').
  2. Step 2: List unique rows from both tables

    Table1 has (1, 'Alice'), (2, 'Bob'); Table2 has (2, 'Bob'), (3, 'Charlie'). After UNION, duplicates removed, result is (1, 'Alice'), (2, 'Bob'), (3, 'Charlie').
  3. Final Answer:

    Rows: (1, 'Alice'), (2, 'Bob'), (3, 'Charlie') -> Option B
  4. Quick Check:

    UNION removes duplicates = Rows: (1, 'Alice'), (2, 'Bob'), (3, 'Charlie') [OK]
Quick Trick: UNION removes duplicates from combined results [OK]
Common Mistakes:
  • Expecting duplicates to appear
  • Confusing UNION with UNION ALL
  • Ignoring duplicate rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes