Bird
0
0

Consider these tables:

medium📝 query result Q5 of 15
SQL - Advanced Joins
Consider these tables:

Employees:
EmpID | DeptID
1 | 10
2 | 20
3 | NULL

Departments:
DeptID | DeptName
10 | Sales
30 | Marketing

What will be the output of this query?
SELECT E.EmpID, D.DeptName FROM Employees E FULL OUTER JOIN Departments D ON E.DeptID = D.DeptID ORDER BY E.EmpID;
A1 | Sales<br>2 | NULL<br>3 | NULL
B1 | Sales<br>2 | NULL<br>3 | NULL<br>NULL | Marketing
C1 | Sales<br>2 | NULL<br>NULL | Marketing
D1 | Sales<br>2 | NULL<br>3 | NULL<br>30 | Marketing
Step-by-Step Solution
Solution:
  1. Step 1: Match Employees and Departments on DeptID

    EmpID 1 matches DeptID 10 (Sales). EmpID 2 has DeptID 20 which is missing in Departments. EmpID 3 has NULL DeptID.
  2. Step 2: Include unmatched rows from both tables

    Department 30 (Marketing) has no matching employee, so appears with NULL EmpID.
  3. Final Answer:

    1 | Sales
    2 | NULL
    3 | NULL
    NULL | Marketing
    -> Option B
  4. Quick Check:

    FULL OUTER JOIN shows all rows from both tables with NULLs where unmatched = A [OK]
Quick Trick: NULL keys cause unmatched rows to appear with NULLs [OK]
Common Mistakes:
MISTAKES
  • Ignoring unmatched rows from Departments
  • Assuming NULL matches NULL in join

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes