Is there any efficiency difference in an explicit vs implicit inner join? For example:
select * from
table a inner join table b
on a.id = b.id;
vs.
select a.*, b.*
from table a, table b
where a.id = b.id;
Is there any efficiency difference in an explicit vs implicit inner join? For example:
select * from
table a inner join table b
on a.id = b.id;
vs.
select a.*, b.*
from table a, table b
where a.id = b.id;
Performance wise, they are exactly the same (at least in SQL Server) but be aware that they are deprecating the implicit outer join syntax.
Personally I prefer the join syntax as its makes it clearer that the tables are joined and how they are joined. Try compare larger SQL queries where you selecting from 8 different tables and you have lots of filtering in the where. By using join syntax you separate out the parts where the tables are joined, to the part where you are filtering the rows.
@lomaxx, just for clarity's sake, could you specify which syntax of the 2 in the question is deprecated?
(thanks, you're right it isn't overly clear. It's now been updated)
Performance wise, they are exactly the same (at least in SQL Server) but be aware that they are deprecating this join syntax and it's not supported by sql server2005 out of the box.
I think you are thinking of the deprecated *= and =* operators vs. "outer join".
I have just now tested the two formats given, and they work properly on a SQL Server 2008 database. In my case they yielded identical execution plans, but I couldn't confidently say that this would always be true.
On some databases (notably Oracle) the order of the joins can make a huge difference to query performance (if there are more than two tables). On one application, we had literally two orders of magnitude difference in some cases. Using the inner join syntax gives you control over this - if you use the right hints syntax.
You didn't specify which database you're using, but probability suggests SQL Server or MySQL where there it makes no real difference.
@lomaxx: Just to clarify, I'm pretty certain that both above syntax are supported by SQL Serv 2005. The syntax below is NOT supported however
select a., b.
from table a, table b
where a.id *= b.id;
Specifically, the outer join (*=) is not supported.
The first answer you gave uses what is known as ANSI join syntax, the other is valid and will work in any relational database.
I agree with grom that you should use ANSI join syntax. As they said, the main reason is for clarity. Rather than having a where clause with lots of predicates, some of which join tables and others restricting the rows returned with the ANSI join syntax you are making it blindingly clear which conditions are being used to join your tables and which are being used to restrict the results.
As Leigh Caldwell has stated, the query optimizer can produce different query plans based on what functionally looks like the same SQL statement. For further reading on this, have a look at the following two blog postings:-
One posting from the Oracle Optimizer Team
Another posting from the "Structured Data" blog
I hope you find this interesting.
The second syntax has the unwanted possibility of a cross join: you can add tables to the FROM part without corresponding WHERE clause. This is considered harmful.