At times, the following statement is confusing, at least to me, though I have been working with joins for a long time.
The part which is confusing here is, considering second left join
SELECT * FROM table1
LEFT JOIN table2 ON table1.id=table2.id
LEFT JOIN table3 ON table2.id=table3.id;
-
Which will be considered as left table – either table1 or table2 (table2 is involved in join condition).
-
If I recall correctly, left join result involves matched results from join condition and unmatched results from left table. If left table is table1 (from point 1.), what will be unmatched rows from table1, as it isn’t involved in the join condition.
The query will return all of the rows that are in
Table1. It will join the rows fromTable2only where theidvalues are equal betweenTable1andTable2. It will also join the rows fromTable3where theidvalues are equal between the result of the join of (Table1ANDTable2) andTable3. So if the first join betweenTable1andTable2has produced a NULL result (no equivalentidvalue inTable2), then the second join will also produce a NULL result. For example (using SQL Server):This query will produce the following results:
To answer your question,
Table1is involved on the second join because the second join will only contain rows fromTable2that were able to be joined withTable1. In other words, the second join isn’tTable2toTable3, it’s the result of the left join ofTable1andTable2toTable3. So in my example, even thoughTable2andTable3both have records with anIDof 5, those records are not included in the result set becauseTable1does not have a record with anIDof 5.