两张空表执行Inner Join是否等同于Cross Join操作?
Great question! Let’s cut straight to the answer first: Yes, when you run an INNER JOIN or CROSS JOIN on two completely empty tables, the result will be the same—an empty result set with zero rows.
Let’s break down why this makes sense:
Cross Join Basics: A cross join returns the Cartesian product of the two tables. That means every row from the first table is paired with every row from the second. If Table A has 0 rows and Table B has 0 rows, the product is
0 * 0 = 0rows—so you get nothing back.Inner Join Basics: An inner join starts by computing the Cartesian product of the two tables, then filters the results to only keep rows that match your join condition. But if the initial Cartesian product has 0 rows (because both tables are empty), there’s nothing to filter. No matter what join condition you use (even something like
1=1), you still end up with 0 rows.
Here’s a quick SQL example to prove it:
-- Create two empty tables CREATE TABLE EmptyTable1 (id INT); CREATE TABLE EmptyTable2 (value VARCHAR(50)); -- Cross Join SELECT * FROM EmptyTable1 CROSS JOIN EmptyTable2; -- Result: 0 rows returned -- Inner Join (with a typical condition) SELECT * FROM EmptyTable1 INNER JOIN EmptyTable2 ON EmptyTable1.id = EmptyTable2.value; -- Result: 0 rows returned -- Even with an always-true condition SELECT * FROM EmptyTable1 INNER JOIN EmptyTable2 ON 1=1; -- Result: Still 0 rows returned
The key takeaway here is that when both tables are empty, there’s no data to pair or filter—so both operations end up producing the same empty result set.
内容的提问来源于stack exchange,提问作者Mrinal

