That all depends, if you assume that every row matches at most one other row, then the min number of rows is 0 and the max number of rows is min(m, n)
. If it is possible for a row from A to match with multiple rows from B, then the max explodes to m * n
, if every row in A matches every row in B.
The following returns 3 rows, since the matches are direct.
WITH a(id, name) AS (
SELECT *
FROM (VALUES (1, 'Ringo'),
(2, 'George'),
(3, 'Paul'),
(4, 'John')) as a
), b(id, food) AS (
SELECT *
FROM (VALUES (1, 'eggs'),
(2, 'ham'),
(3, 'spam')) as b
)
SELECT *
FROM a
INNER JOIN b ON a.id = b.id;
+--+------+--+---------+
|id|name |id|food |
+--+------+--+---------+
|1 |Ringo |1 |Eggs |
|2 |George|2 |Ham |
|3 |Paul |3 |Spam |
+--+------+--+---------+
But this returns many more rows.
WITH a(id, name) AS (
SELECT *
FROM (VALUES (1, 'Ringo'),
(2, 'George'),
(3, 'Paul'),
(4, 'John')) as a
), b(id, food) AS (
SELECT *
FROM (VALUES (1, 'Eggs'),
(2, 'Ham'),
(3, 'Spam')) as b
)
SELECT *
FROM a
INNER JOIN b ON b.food <= a.name
+--+------+--+---------+
|id|name |id|food |
+--+------+--+---------+
|1 |Ringo |1 |Eggs |
|2 |George|1 |Eggs |
|3 |Paul |1 |Eggs |
|4 |John |1 |Eggs |
|1 |Ringo |2 |Ham |
|3 |Paul |2 |Ham |
|4 |John |2 |Ham |
+--+------+--+---------+