0
0
MySQLquery~10 mins

RIGHT JOIN in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - RIGHT JOIN
Start with LEFT table
Match rows in RIGHT table
Include all rows from RIGHT table
If no match, LEFT columns NULL
Combine rows into result
Output final joined table
RIGHT JOIN returns all rows from the right table and matched rows from the left table. If no match, left side is NULL.
Execution Sample
MySQL
SELECT A.id, A.name, B.city
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 based on id and a_id.
Execution Table
StepActionLeft Table RowRight Table RowMatch ConditionResult Row
1Check B row with a_id=1A.id=1, name='Ann'B.a_id=1, city='NY'1=1 (True)id=1, name='Ann', city='NY'
2Check B row with a_id=2A.id=2, name='Bob'B.a_id=2, city='LA'2=2 (True)id=2, name='Bob', city='LA'
3Check B row with a_id=4No matching A rowB.a_id=4, city='SF'NULL=4 (False)id=NULL, name=NULL, city='SF'
4No more B rowsN/AN/AN/AEND
💡 All rows from right table B processed; unmatched left rows set to NULL.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Current B rowNonea_id=1, city='NY'a_id=2, city='LA'a_id=4, city='SF'None
Current A rowNoneid=1, name='Ann'id=2, name='Bob'No match (NULL)None
Result rowNoneid=1, name='Ann', city='NY'id=2, name='Bob', city='LA'id=NULL, name=NULL, city='SF'None
Key Moments - 2 Insights
Why do some columns from the left table show NULL in the result?
Because RIGHT JOIN includes all rows from the right table even if there is no matching row in the left table, so left table columns are NULL for unmatched rows (see execution_table row 3).
Does RIGHT JOIN include all rows from the left table?
No, RIGHT JOIN includes all rows from the right table only. Left table rows appear only if they match (see execution_table rows 1 and 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result row at step 3?
Aid=4, name='Dan', city='SF'
Bid=NULL, name=NULL, city='SF'
Cid=3, name='Cara', city=NULL
DNo result row
💡 Hint
Check execution_table row 3 under 'Result Row' column.
At which step does the join find no matching left table row?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'No matching A row' in execution_table under 'Left Table Row'.
If we changed RIGHT JOIN to LEFT JOIN, which table's rows would all appear?
AAll rows from table A (left table)
BAll rows from table B (right table)
COnly matching rows from both tables
DNo rows at all
💡 Hint
RIGHT JOIN includes all right table rows; LEFT JOIN includes all left table rows.
Concept Snapshot
RIGHT JOIN syntax:
SELECT columns
FROM left_table RIGHT JOIN right_table
ON join_condition;

Returns all rows from right_table,
matching rows from left_table,
NULLs if no match on left side.
Full Transcript
RIGHT JOIN in SQL returns all rows from the right table and matching rows from the left table. If a row in the right table has no matching row in the left table, the left table columns are filled with NULL. The execution flow starts by scanning each row in the right table, then tries to find matching rows in the left table based on the join condition. Matched rows are combined into the result. Unmatched right table rows still appear with NULLs for left table columns. This is different from LEFT JOIN, which returns all rows from the left table instead. The example query selects id and name from table A and city from table B, joining on A.id = B.a_id. The execution table shows step-by-step how each row from B is matched or not matched with A, and how the result rows are formed.