0
0
SQLquery~10 mins

LEFT JOIN vs RIGHT JOIN decision in SQL - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - LEFT JOIN vs RIGHT JOIN decision
Start with LEFT JOIN
Include all rows from Left Table
Match rows from Right Table
If no match, fill Right Table columns with NULL
Result: All Left Table rows + matched Right Table rows
Start with RIGHT JOIN
Include all rows from Right Table
Match rows from Left Table
If no match, fill Left Table columns with NULL
Result: All Right Table rows + matched Left Table rows
LEFT JOIN keeps all rows from the left table, matching right table rows or NULLs; RIGHT JOIN keeps all rows from the right table, matching left table rows or NULLs.
Execution Sample
SQL
SELECT A.id, B.value
FROM A
LEFT JOIN B ON A.id = B.id;
This query returns all rows from table A, and matching rows from B; if no match, B columns are NULL.
Execution Table
StepActionLeft Table RowRight Table RowResult RowNotes
1Take first row from AA.id=1Find B.id=11, B.value=100Match found, include both
2Take second row from AA.id=2Find B.id=22, B.value=NULLNo match in B, fill NULL
3Take third row from AA.id=3Find B.id=33, B.value=300Match found, include both
4No more rows in ALEFT JOIN ends after all A rows processed
💡 All rows from left table A processed; right table B matched or NULL filled
Variable Tracker
VariableStartAfter 1After 2After 3Final
Current A rowNoneid=1id=2id=3None
Matched B rowNoneid=1,value=100Noneid=3,value=300None
Result rowNone(1,100)(2,NULL)(3,300)All rows processed
Key Moments - 2 Insights
Why does the second result row have NULL for B.value?
Because in execution_table row 2, A.id=2 has no matching B.id=2, so LEFT JOIN fills B columns with NULL.
What happens if we switch to RIGHT JOIN?
The join would include all rows from B, matching A rows or NULL if no match, reversing the role of tables as shown in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Result Row at Step 3?
A(3, NULL)
B(3, 300)
C(3, 100)
D(NULL, 300)
💡 Hint
Check execution_table row 3 under Result Row column
At which step does the LEFT JOIN fill NULL for the right table columns?
AStep 1
BStep 3
CStep 2
DNo NULLs filled
💡 Hint
Look at execution_table row 2 Notes and Result Row
If we change the query to RIGHT JOIN, which table's rows will all appear in the result?
ARight table B
BLeft table A
COnly matching rows
DNo rows
💡 Hint
Refer to concept_flow description about RIGHT JOIN behavior
Concept Snapshot
LEFT JOIN returns all rows from the left table plus matched rows from the right table; unmatched right rows show NULL.
RIGHT JOIN returns all rows from the right table plus matched rows from the left table; unmatched left rows show NULL.
Use LEFT JOIN when you want to keep all left table data.
Use RIGHT JOIN when you want to keep all right table data.
Both joins match rows based on the ON condition.
If no match, columns from the other table are NULL.
Full Transcript
This visual execution shows how LEFT JOIN and RIGHT JOIN work in SQL. LEFT JOIN keeps all rows from the left table and matches rows from the right table. If no match is found, the right table columns are filled with NULL. The execution table traces each row from the left table, showing matches or NULLs. The variable tracker shows how the current rows and results change step-by-step. Key moments clarify why NULLs appear and what changes if we switch to RIGHT JOIN. The quiz tests understanding of the join results and behavior. The concept snapshot summarizes the key differences and when to use each join type.