I have 2 tables A and B. Table A has 3 rows and one column having values 1, 1, 1. Table B has 2 rows and one column having values 1, 1. Upon applying left join, Table A left join Table B The output I will receive is: 1, 1, 1.
Is it correct?
Advertisement
Answer
No, your assumption is not correct. If you join on the columns you mention, you will get 6 rows (3 x 2), because every row matches each other.
Table t1
id_t1 | colx |
---|---|
1 | 1 |
2 | 1 |
3 | 1 |
Table t2
id_t2 | coly |
---|---|
10 | 1 |
20 | 1 |
Query
select * from t1 left outer join t2 on t2.coly = t1.colx order by t1.id_t1, t2.id_t2;
Result
id_t1 | colx | id_t2 | coly |
---|---|---|---|
1 | 1 | 10 | 1 |
1 | 1 | 20 | 1 |
2 | 1 | 10 | 1 |
2 | 1 | 20 | 1 |
3 | 1 | 10 | 1 |
3 | 1 | 20 | 1 |
You seem to misunderstand what a left outer join does. Each row in table t1 has two matches in table t2. You get these with a left outer join, but you also get them with an inner join. The difference between the two join types is when there is no match. Let’s add another row to table t1:
id_t1 | colx |
---|---|
4 | 2 |
There is no match in t2 for the value 2. An inner join would not show this row in the results. A left outer join in contrast will add the following row to your results:
id_t1 | colx | id_t2 | coly |
---|---|---|---|
4 | 2 | null | null |