Reputation: 12660
What is it wrong about this simple SQL outer join?
select count(*) from A -- 25766
select count(*) from B -- 1242
select count(*) from A left outer join B on A.b = B.b -- 310176
return 25766, 1242 and 310176 rows respectively. (This is for Microsoft SQL Server 2012.) How can A left outer join B
ever return more rows than exist in A
, especially given this Venn diagram? I guess I'm making a stupid mistake but what is it?
Upvotes: 7
Views: 6194
Reputation: 272106
There is nothing strange about it (and this situation applies to inner joins too). A left outer join:
So at minimum the query will return 25766 rows but there could be more. It is possible to have one row in table A that matches many rows in table B. Example:
A B Result
+----+----+ +-----+----+ +------+-----+-----+------+
| id| b| | id| b| | a.id| a.b| b.b| b.id|
+----+----+ +-----+----+ +------+-----+-----+------+
| 1| 10| | 123| 10| | 1| 10| 10| 123|
+----+----+ | 456| 10| | 1| 10| 10| 456|
+-----+----+ +------+-----+-----+------+
This returns two rows even though there is one row in table A.
Upvotes: 3
Reputation:
This can happen when column b
is not unique in table B
. Suppose you have this data:
A B +---+ +---+---+ | b | | b | c | +---+ +---+---+ | 1 | | 2 | 1 | | 2 | | 2 | 2 | +---+ +---+---+
When you left-join from A
to B
on column b
, you get
+-----+------+------+ | A.b | B.b | B.c | +-----+------+------+ | 1 | NULL | NULL | | 2 | 2 | 1 | | 2 | 2 | 2 | +-----+------+------+
which gives three rows in total, even though both A
and B
only have two rows each.
Upvotes: 16