SQL Server 2012 – Using Two LEFT JOINs vs Single LEFT JOIN with AND

join;sql-server-2012

Using two LEFT JOINs i.e.

SELECT <some columns>
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 ON <condition1>
LEFT JOIN Table2 AS t3 ON <condition2>

is this same as using AND in single LEFT JOIN? i.e.

SELECT <some columns>
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 ON <condition1>
AND <condition2>

Both are same or different(in general)?

Best Answer

They are different. In the first option you get 2 times Table2 into your query. Once as t2 and once as t3. Both have a different content and you must put them somehow back together. To me this is more an OR instead of an AND. In the second option you get only the Table2 rows that meet both criteria.

Suppose you have Table2 with the following content:

| Color | Size |
|-------|------|
| Red   |  S   |
| Blue  |  S   |
| Blue  |  XS  |

Suppose you want to have the rows that are Blue and S. In your first option you get all rows (t2 for example with all Blue and t3 with all S) and in your second option you only get row 2.