0
0
SQLquery~10 mins

RIGHT JOIN execution behavior in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - RIGHT JOIN execution behavior
Start with RIGHT table rows
Match rows in LEFT table
Combine rows
Output result
RIGHT JOIN returns all rows from the right table and matched rows from the left table; unmatched right rows show NULLs.
Execution Sample
SQL
SELECT A.id, A.name, B.score
FROM A
RIGHT JOIN B ON A.id = B.a_id;
This query returns all rows from table B and matching rows from table A, filling NULLs where no match exists.
Execution Table
StepCurrent B rowMatching A row?ActionOutput row
1B.a_id=1, score=90A.id=1, name='Alice'Match found, combine rows(1, 'Alice', 90)
2B.a_id=2, score=85A.id=2, name='Bob'Match found, combine rows(2, 'Bob', 85)
3B.a_id=4, score=70No matching A.id=4No match, fill A columns with NULL(NULL, NULL, 70)
4No more rows in BEndStop executionQuery complete
💡 All rows from B processed; unmatched B rows included with NULLs for A columns.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Current B rowNonea_id=1, score=90a_id=2, score=85a_id=4, score=70None
Matching A rowNoneid=1, name='Alice'id=2, name='Bob'No matchNone
Output rows count01233
Key Moments - 2 Insights
Why do we get NULLs in the output for some columns?
When a row in the right table has no matching row in the left table, LEFT columns are filled with NULLs, as shown in step 3 of the execution_table.
Does RIGHT JOIN include all rows from the left table?
No, RIGHT JOIN includes all rows from the right table. Left table rows without matching right rows are excluded, as seen in the execution_table where only B rows drive output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output row at step 2?
A(2, NULL, 85)
B(NULL, NULL, 85)
C(2, 'Bob', 85)
D(NULL, 'Bob', 85)
💡 Hint
Check the 'Output row' column for step 2 in the execution_table.
At which step does the RIGHT JOIN output a row with NULLs for the left table columns?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for 'No match, fill A columns with NULL' in the 'Action' column.
If table B had no rows, what would the output be?
AAll rows from A with NULLs for B columns
BNo rows returned
CAll rows from B with NULLs for A columns
DError due to missing data
💡 Hint
RIGHT JOIN returns all rows from the right table; if right table is empty, no rows output.
Concept Snapshot
RIGHT JOIN syntax:
SELECT columns
FROM left_table
RIGHT JOIN right_table ON condition;

Behavior:
- Returns all rows from right_table
- Matches rows from left_table
- NULLs for left_table columns if no match

Use when you want all right_table data regardless of left_table matches.
Full Transcript
RIGHT JOIN returns all rows from the right table and matching rows from the left table. For each row in the right table, the database looks for matching rows in the left table based on the join condition. If a match is found, it combines the rows. If no match is found, it includes the right table row with NULLs in the left table columns. This ensures all right table rows appear in the result. The execution table shows step-by-step how each right table row is processed, matched, and output. NULLs appear when no left table match exists. If the right table is empty, no rows are returned. This join is useful when you want to keep all data from the right table and add matching left table data if available.