While writing in SQL, how would I know if I should use cross product (cross join, Cartesian product) or natural join?
-
http://sqlfiddle.com/#!15/223f7/1 – Oct 11 '14 at 18:35
-
Possible duplicate of [Relational Algebra - Cartesian Product vs Natural Join?](https://stackoverflow.com/questions/14127306/relational-algebra-cartesian-product-vs-natural-join) – philipxy Jul 14 '19 at 01:04
-
@philipxy Honestly, I might be more inclined to close the other one as a dupe of this one. The other one gets into a lot of minutia about a mental model of the theoretical concept and largely ignores the practical considerations. In the process, it adds a lot of unnecessary complexity. – jpmc26 Jul 14 '19 at 06:17
3 Answers
CROSS JOIN
creates all possible pairings of rows from two tables, whether they match or not. You don't use any join condition for a cross product, because the condition would always be true for any pairing.
An example of using CROSS JOIN
: you have tables of ShoeColors and ShoeSizes, and you want to know how many possible combinations there are. SELECT COUNT(*) FROM ShoeColors CROSS JOIN ShoeSizes;
NATURAL JOIN
is just like an INNER JOIN
, but it assumes the condition is equality and applies for all columns names that appear in both tables. I never use NATURAL JOIN
, because I can't assume that just because columns have the same name, that they should be related. That would require a very strict column naming convention, and practically no real-world project has such discipline.

- 538,548
- 86
- 673
- 828
-
The only time I might use `NATURAL JOIN` is in an exploratory throw-away query. When I'm trying to view data manually and directly using a SQL client and the query I'm writing will never need to be maintained and I'm confident for that specific situation and the consequences of a mistake are virtually nothing, I might use it just to save myself some typing. – jpmc26 Jul 14 '19 at 06:15
In simple words,cross join performed a cartesian product among the rows of two different tables..where the columns name may or may not be matched......but in natural join its mandatory that in order to perform join operation columns name of two tables must be matched
Natural join is a cross join with where condition being on columns with same name from both tables.
Let's assume that you have two tables: A and B
>>select * from A;
col1 col2 col3
---------------
1 2 1
1 4 0
>>select * from B;
col1 col2
---------
1 2
1 3
>>select * from A cross join B;
col1 col2 col3 col1_2 col2_2
-----------------------------
1 2 1 1 2
1 2 1 1 3
1 4 0 1 2
1 4 0 1 3
>>select * from A natural join B;
col1 col2 col3
---------------
1 2 1
1 2 1
For further details visit my post. https://eduit.nuxaavi.com.mx/2021/10/16/producto-cartesiano-sql/