0
0
SQLquery~10 mins

Finding unmatched rows with LEFT JOIN in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Finding unmatched rows with LEFT JOIN
Start with LEFT JOIN
Match rows from left table
Check if right table row exists
Yes No
Keep combined row
Result set
LEFT JOIN keeps all rows from the left table and matches rows from the right table. If no match, right side columns are NULL.
Execution Sample
SQL
SELECT A.id, B.id
FROM A
LEFT JOIN B ON A.id = B.a_id
WHERE B.a_id IS NULL;
This query finds rows in table A that have no matching row in table B.
Execution Table
StepA.idB.a_idB.idJoin Condition (A.id = B.a_id)ActionOutput Row Included?
111101=1 TrueMatch found, combine rowsYes
22NULLNULLNo matching B rowNo match, keep A row with NULLsYes
333303=3 TrueMatch found, combine rowsYes
44NULLNULLNo matching B rowNo match, keep A row with NULLsYes
5Filter WHERE B.a_id IS NULLKeep only rows where B.a_id is NULLRows with A.id 2 and 4
💡 All rows from A processed; final filter keeps only unmatched rows where B.a_id is NULL
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
A.idN/A1234N/A
B.a_idN/A1NULL3NULLFiltered NULL only
Output RowsEmptyRow with A.id=1 and B.id=10Row with A.id=2 and B.id=NULLRow with A.id=3 and B.id=30Row with A.id=4 and B.id=NULLRows with A.id=2 and 4 only
Key Moments - 2 Insights
Why do some rows have NULL values in the right table columns after LEFT JOIN?
Because those left table rows have no matching row in the right table, so SQL fills right side columns with NULLs (see execution_table rows 2 and 4).
Why do we use WHERE B.a_id IS NULL to find unmatched rows?
Because unmatched rows have NULL in right table columns, filtering on B.a_id IS NULL keeps only those unmatched rows (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of B.a_id at step 2?
A1
BNULL
C2
D10
💡 Hint
Check the 'B.a_id' column in execution_table row 2
At which step does the join condition fail, resulting in NULLs for right table columns?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'No matching B row' in the 'Action' column of execution_table
If we remove the WHERE clause filtering B.a_id IS NULL, what rows will the output include?
AAll rows from A with matched or unmatched B rows
BOnly rows without matches in B
COnly rows with matches in B
DNo rows at all
💡 Hint
LEFT JOIN keeps all rows from A regardless of matches; WHERE clause filters unmatched rows
Concept Snapshot
LEFT JOIN returns all rows from the left table.
If no match in right table, right columns are NULL.
Use WHERE right_table.key IS NULL to find unmatched rows.
This helps find rows in left table without matches in right table.
Full Transcript
This visual execution shows how LEFT JOIN works to find unmatched rows. We start by joining tables A and B on matching IDs. For each row in A, SQL tries to find matching rows in B. If a match exists, it combines the rows. If not, it keeps the A row and fills B columns with NULL. Then, filtering WHERE B.a_id IS NULL keeps only those unmatched rows from A. The execution table traces each step, showing variable values and actions. This helps beginners see why NULLs appear and how filtering finds unmatched rows.