Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using a cross join and putting a comma between the two tables?

What is the difference between

select * from A, B 

and

select * from A cross join B 

? They seem to return the same results.

Is the second version preferred over the first? Is the first version completely syntactically wrong?

like image 885
dmr Avatar asked Oct 12 '10 20:10

dmr


People also ask

Is Cross join the same as comma?

Using multiple tables sepparated by a comma, is an old syntax which should not be used anymore. (Although it is still supported). I used to say that the comma should be replaced by a CROSS JOIN and that although the syntax is different, the effect 'should' be the same.

Why would you use a cross join?

The CROSS JOIN is used to generate a paired combination of each row of the first table with each row of the second table. This join type is also known as cartesian join. Suppose that we are sitting in a coffee shop and we decide to order breakfast.

What is the difference between cross and join?

It depends on the output you expect. A cross join matches all rows in one table to all rows in another table. An inner join matches on a field or fields. If you have one table with 10 rows and another with 10 rows then the two joins will behave differently.

Can we use comma instead of join?

INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).


2 Answers

They return the same results because they are semantically identical. This:

select *    from A, B 

...is (wince) ANSI-89 syntax. Without a WHERE clause to link the tables together, the result is a cartesian product. Which is exactly what alternative provides as well:

    select *        from A  cross join B 

...but the CROSS JOIN is ANSI-92 syntax.

About Performance

There's no performance difference between them.

Why Use ANSI-92?

The reason to use ANSI-92 syntax is for OUTER JOIN support (IE: LEFT, FULL, RIGHT)--ANSI-89 syntax doesn't have any, so many databases implemented their own (which doesn't port to any other databases). IE: Oracle's (+), SQL Server's =*

like image 178
OMG Ponies Avatar answered Oct 05 '22 11:10

OMG Ponies


Stumbled upon this post from another SO question, but a big difference is the linkage cross join creates. For example using cross apply or another join after B on the first ('comma') variant, the cross apply or join would only refer to the table(s) after the dot. e.g, the following:

select * from A, B join C on C.SomeField = A.SomeField and C.SomeField = B.SomeField  

would create an error:

The multi-part identifier "A.SomeField" could not be bound.

because the join on C only scopes to B, whereas the same with cross join...

select * from A cross join B join C on C.SomeField = A.SomeField and C.SomeField = B.SomeField  

..is deemed ok. The same would apply if cross apply is used. For example placing a cross apply on a function after B, the function could only use fields of B, where the same query with cross join, could use fields from both A and B. Of course, this also means the reverse can be used as well. If you want to add a join solely for one of the tables, you can achieve that by going 'comma' on the tables.

like image 28
Me.Name Avatar answered Oct 05 '22 12:10

Me.Name