0
0
SQLquery~10 mins

Multiple LEFT JOINs in one query in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple LEFT JOINs in one query
Start with main table
LEFT JOIN first table
Combine rows, keep all from main
LEFT JOIN second table
Combine rows, keep all from main
Result with all rows from main and matching rows from joined tables
END
Start with a main table, then add rows from other tables using LEFT JOINs one by one, keeping all rows from the main table even if no match is found.
Execution Sample
SQL
SELECT A.id, B.name, C.status
FROM A
LEFT JOIN B ON A.id = B.a_id
LEFT JOIN C ON A.id = C.a_id;
This query selects data from table A and adds matching data from tables B and C using LEFT JOINs.
Execution Table
StepActionTables InvolvedRows ResultingExplanation
1Start with table AA3 rowsInitial rows from A: ids 1, 2, 3
2LEFT JOIN B on A.id = B.a_idA, B3 rowsRows from A kept; B data added where matches exist (id=1,2) else NULL
3LEFT JOIN C on A.id = C.a_idA, B, C3 rowsRows from previous step kept; C data added where matches exist (id=2,3) else NULL
4Final resultA, B, C3 rowsAll rows from A with matching B and C data or NULLs where no match
💡 All rows from A are included; LEFT JOINs add matching rows from B and C or NULL if no match
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Rows3 rows from A3 rows with B data or NULL3 rows with B and C data or NULL3 rows with combined data
Key Moments - 2 Insights
Why do we still have 3 rows after the LEFT JOINs even if some joined tables have no matching rows?
Because LEFT JOIN keeps all rows from the main table (A) regardless of matches in joined tables, as shown in execution_table rows 2 and 3.
What happens to columns from joined tables when there is no matching row?
Those columns show NULL values, as seen in execution_table row 2 for id=3 in table B and row 3 for id=1 in table C.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many rows are in the result after the first LEFT JOIN?
A2 rows
B3 rows
C5 rows
D0 rows
💡 Hint
Check execution_table row 2 under 'Rows Resulting'
At which step do columns from table C get added to the result?
AStep 1
BStep 2
CStep 3
DFinal step only
💡 Hint
Look at execution_table row 3 'Action' column
If table B had no matching rows for any A.id, what would happen to the number of rows after the first LEFT JOIN?
ANumber of rows would stay the same
BNumber of rows would increase
CNumber of rows would decrease
DQuery would fail
💡 Hint
LEFT JOIN keeps all rows from the main table as shown in execution_table row 2
Concept Snapshot
Multiple LEFT JOINs combine one main table with several others.
Syntax: SELECT ... FROM main LEFT JOIN table1 ON ... LEFT JOIN table2 ON ...
All rows from main table stay in result.
Joined tables add matching data or NULL if no match.
Useful to gather related info without losing main rows.
Full Transcript
This visual execution shows how multiple LEFT JOINs work in SQL. We start with a main table A with 3 rows. Then we LEFT JOIN table B on matching ids, keeping all rows from A and adding B's data where it matches, else NULL. Next, we LEFT JOIN table C similarly. The final result has all rows from A with matching data from B and C or NULLs if no match. Key points: LEFT JOIN keeps all main rows; unmatched joined columns become NULL. This helps combine data from multiple tables without losing main table rows.