HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Using Two LEFT JOIN or use AND with single LEFT JOIN?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
leftwithjointwosingleusinganduse

Problem

Using two LEFT JOINs i.e.

SELECT 
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 ON 
LEFT JOIN Table2 AS t3 ON 


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

SELECT 
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 ON 
AND 


Both are same or different(in general)?

Solution

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.

Code Snippets

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

Context

StackExchange Database Administrators Q#142018, answer score: 12

Revisions (0)

No revisions yet.