0
0
PostgreSQLquery~10 mins

FULL OUTER JOIN in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - FULL OUTER JOIN
Start with Table A
Match rows on join condition
Keep matched rows from both
Add unmatched rows from A with NULLs for B
Add unmatched rows from B with NULLs for A
Combine all rows into result
Output Result
FULL OUTER JOIN combines rows from both tables matching on a condition, including unmatched rows from both sides with NULLs where no match exists.
Execution Sample
PostgreSQL
SELECT A.id, A.name, B.id, B.city
FROM A
FULL OUTER JOIN B ON A.id = B.id;
This query joins tables A and B on id, returning all rows from both tables, matching where ids are equal, and filling NULLs where no match.
Execution Table
StepTable A RowTable B RowJoin Condition (A.id = B.id)ActionOutput Row
1A: (1, 'Alice')B: (1, 'NY')TrueMatch rows(1, 'Alice', 1, 'NY')
2A: (2, 'Bob')B: (2, 'LA')TrueMatch rows(2, 'Bob', 2, 'LA')
3A: (3, 'Charlie')B: NULLFalseNo match, add A row with NULLs for B(3, 'Charlie', NULL, NULL)
4A: NULLB: (4, 'Chicago')FalseNo match, add B row with NULLs for A(NULL, NULL, 4, 'Chicago')
5End of tablesStopAll rows combined
💡 All rows from both tables processed; unmatched rows included with NULLs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Output Rows[][(1, 'Alice', 1, 'NY')][(1, 'Alice', 1, 'NY'), (2, 'Bob', 2, 'LA')][(1, 'Alice', 1, 'NY'), (2, 'Bob', 2, 'LA'), (3, 'Charlie', NULL, NULL)][(1, 'Alice', 1, 'NY'), (2, 'Bob', 2, 'LA'), (3, 'Charlie', NULL, NULL), (NULL, NULL, 4, 'Chicago')][(1, 'Alice', 1, 'NY'), (2, 'Bob', 2, 'LA'), (3, 'Charlie', NULL, NULL), (NULL, NULL, 4, 'Chicago')]
Key Moments - 2 Insights
Why do some rows have NULL values in the output?
Rows with NULLs come from one table that has no matching row in the other table, as shown in steps 3 and 4 of the execution_table.
Does FULL OUTER JOIN only return matched rows?
No, it returns all matched rows plus unmatched rows from both tables with NULLs for missing parts, as seen in the combined output after step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output row at step 3?
A(3, 'Charlie', 3, 'Chicago')
B(3, 'Charlie', NULL, NULL)
C(NULL, NULL, 3, 'Charlie')
D(NULL, 'Charlie', NULL, NULL)
💡 Hint
Check the 'Output Row' column for step 3 in the execution_table.
At which step does the join condition become false because there is no matching row in Table B?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Join Condition' column in the execution_table where it is False and Table B Row is NULL.
If Table A had an extra row with id=5 and no matching id in Table B, what would happen in the output?
AA new output row with id=5 and NULLs for Table B columns would be added
BNo change, because FULL OUTER JOIN only shows matched rows
CThe query would error out
DThe row would be ignored
💡 Hint
FULL OUTER JOIN includes unmatched rows from both tables with NULLs, as shown in steps 3 and 4.
Concept Snapshot
FULL OUTER JOIN syntax:
SELECT columns
FROM A
FULL OUTER JOIN B ON A.key = B.key;

Returns all rows from A and B,
matching where keys equal,
NULLs where no match exists.
Full Transcript
FULL OUTER JOIN takes two tables and combines them by matching rows on a condition. It keeps all rows from both tables. When a row in one table has no match in the other, it still appears in the result with NULLs for the missing side. This is useful to see all data from both tables together, including unmatched rows. The example query joins tables A and B on id, showing matched rows and unmatched rows with NULLs. The execution table shows step-by-step how rows are matched or added with NULLs. Key points are that unmatched rows appear with NULLs, and the join condition controls matching. This helps beginners understand how FULL OUTER JOIN works visually.