0
0
SQLquery~10 mins

NULLs in JOIN conditions in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - NULLs in JOIN conditions
Start with two tables
Perform JOIN
Compare join keys
Check if keys are NULL
Yes No
No match
Return joined rows or NULLs
End
This flow shows how SQL JOIN compares keys, treats NULLs as unknown, and only matches rows when keys are equal and not NULL.
Execution Sample
SQL
SELECT A.id, B.id
FROM A
LEFT JOIN B ON A.id = B.id;
This query joins tables A and B on the id column, showing how NULLs affect matching.
Execution Table
StepA.idB.idJoin Condition (A.id = B.id)Match ResultOutput Row
1111 = 1 (True)Match(1, 1)
22NULL2 = NULL (Unknown)No Match(2, NULL) from A with NULL in B columns
3NULL2NULL = 2 (Unknown)No Match(NULL, NULL) from A with NULL in B columns
4NULLNULLNULL = NULL (Unknown)No Match(NULL, NULL) from A with NULL in B columns
5333 = 3 (True)Match(3, 3)
💡 All rows processed; NULL comparisons never match, so LEFT JOIN returns NULLs for B columns when no match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
A.id112NULLNULL33
B.id11NULL2NULL33
Join Condition ResultN/ATrueUnknownUnknownUnknownTrueTrue
Match ResultN/AMatchNo MatchNo MatchNo MatchMatchMatch
Output RowN/A(1,1)(2,NULL)(NULL,NULL)(NULL,NULL)(3,3)(3,3)
Key Moments - 2 Insights
Why does a join condition comparing NULL to any value not result in a match?
Because in SQL, NULL means unknown, so any comparison with NULL returns unknown (not true). The join only matches when the condition is true, so rows with NULL keys do not match (see execution_table rows 2-4).
Why does a LEFT JOIN still return rows from the left table even when there is no match?
LEFT JOIN returns all rows from the left table. When no match is found on the right, it fills right table columns with NULLs. This is shown in execution_table rows 2-4 where B columns are NULL.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the match result when A.id is 2 and B.id is NULL?
ANo Match
BMatch
CError
DUnknown but treated as Match
💡 Hint
Check row 2 in the execution_table under 'Match Result' column.
At which step does the join condition evaluate to true?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Join Condition (A.id = B.id)' column in the execution_table.
If the join was INNER JOIN instead of LEFT JOIN, what would happen to rows where A.id is 2 and B.id is NULL?
AThey would appear with NULLs for B columns
BThey would be excluded from the output
CThey would cause an error
DThey would match anyway
💡 Hint
INNER JOIN only returns rows where join condition is true; see execution_table rows 2-4 for no matches.
Concept Snapshot
SQL JOIN compares keys to match rows.
NULL means unknown, so NULL = value is never true.
JOIN matches only when condition is true.
LEFT JOIN returns all left rows, filling NULLs for no matches.
NULLs in join keys cause no matches, not errors.
Full Transcript
This visual execution shows how SQL JOIN handles NULLs in join conditions. We start with two tables A and B, each having some NULL values in the join key column 'id'. The JOIN compares A.id and B.id for equality. When either side is NULL, the comparison returns unknown, so no match occurs. For example, when A.id=2 and B.id=NULL, the join condition is unknown, so no match. LEFT JOIN returns all rows from A, and for no matches, fills B columns with NULLs. Only rows where both keys are equal and not NULL match. This explains why NULLs in join keys prevent matching rows in SQL JOINs.