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.
SELECT A.id, A.name, B.score FROM A RIGHT JOIN B ON A.id = B.a_id;
| Step | Current B row | Matching A row? | Action | Output row |
|---|---|---|---|---|
| 1 | B.a_id=1, score=90 | A.id=1, name='Alice' | Match found, combine rows | (1, 'Alice', 90) |
| 2 | B.a_id=2, score=85 | A.id=2, name='Bob' | Match found, combine rows | (2, 'Bob', 85) |
| 3 | B.a_id=4, score=70 | No matching A.id=4 | No match, fill A columns with NULL | (NULL, NULL, 70) |
| 4 | No more rows in B | End | Stop execution | Query complete |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| Current B row | None | a_id=1, score=90 | a_id=2, score=85 | a_id=4, score=70 | None |
| Matching A row | None | id=1, name='Alice' | id=2, name='Bob' | No match | None |
| Output rows count | 0 | 1 | 2 | 3 | 3 |
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.