Bird
0
0

Given these tables:

medium📝 query result Q13 of 15
SQL - Advanced Joins
Given these tables:

Table A:
ID | Name
1 | Alice
2 | Bob
4 | Dana

Table B:
ID | City
2 | Boston
3 | Chicago
4 | Denver

What is the result of this query?
SELECT A.ID, A.Name, B.City FROM A FULL OUTER JOIN B ON A.ID = B.ID ORDER BY A.ID;
A[ (1, 'Alice', NULL), (2, 'Bob', NULL), (3, NULL, 'Chicago'), (4, 'Dana', NULL) ]
B[ (2, 'Bob', 'Boston'), (4, 'Dana', 'Denver') ]
C[ (1, 'Alice', 'Boston'), (2, 'Bob', 'Chicago'), (3, NULL, 'Denver'), (4, 'Dana', NULL) ]
D[ (1, 'Alice', NULL), (2, 'Bob', 'Boston'), (4, 'Dana', 'Denver'), (NULL, NULL, 'Chicago') ]
Step-by-Step Solution
Solution:
  1. Step 1: Match rows by ID using FULL OUTER JOIN

    IDs 2 and 4 appear in both tables, so their rows combine. ID 1 is only in A, ID 3 only in B.
  2. Step 2: Fill NULLs for missing matches

    For ID 1 (A only), City is NULL; for ID 3 (B only), A.ID=NULL, A.Name=NULL, City='Chicago'. ORDER BY A.ID ASC places NULL last: rows for 1,2,4 then NULL row.
  3. Final Answer:

    [ (1, 'Alice', NULL), (2, 'Bob', 'Boston'), (4, 'Dana', 'Denver'), (NULL, NULL, 'Chicago') ] -> Option D
  4. Quick Check:

    FULL OUTER JOIN returns all rows with NULLs for missing matches [OK]
Quick Trick: FULL OUTER JOIN shows all rows, NULLs where no match [OK]
Common Mistakes:
MISTAKES
  • Ignoring unmatched rows from either table
  • Assuming INNER JOIN behavior returns only matches
  • Misordering results by A.ID when NULLs exist

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes