If you don't know which to use, then presumably you area learning SQL. If so, the place to start is with the third one:
select u.firstName, a.[homepage]
from [User] u inner join
[Agent] a
on a.Id = u.Id;
Why? The primary reason is that your intention is to "join" two tables. The ANSI standard syntax for joining uses the JOIN
keyword with the ON
clause. This is the proper, accepted syntax for a JOIN
.
JOIN
is a fundamental part of SQL, because the join operation (or equivalents) is a fundamental part of relational algebra, which is sort of the theoretical foundation of SQL. It is usually among the first things taught in SQL.
That does not make the other two versions "wrong", just misleading. APPLY
implements something called a "lateral join". This is a super-powerful part of the SQL language. However, it is not necessarily the place to begin.
Although you can express all joins using APPLY
, I find that misleading. Perhaps I am old-fashioned and one day, APPLY
will supplant JOIN
. However, SQL has been centered on "regular" joins for decades. And I find them to be useful for expressing relationships between tables.
As for the second version, it is simply archaic. When I see commas in the FROM
clause, I think . . . Gosh that person is old, learned SQL a long, long time ago, and hasn't learned the more powerful, standard, explicit JOIN
syntax. That person probably doesn't even know what an outer join is. How sad.
Don't get confused by execution plans. That is the role of the SQL optimizer. Focus on the query that best represents your intention. If you then have problems with performance, you can work on performance after you have the query that does what you want.