views:

44

answers:

2

This might be a very simple question, but am just curious about it.

Are these queries both representations of equi join?

I find that both returns the same result.

Query1:

SELECT C.* FROM [Case] A, [Event] B, Notice C
WHERE A.CaseID = B.CaseID
AND B.EventID = C.EventID

Query2:

SELECT C.* FROM [Case] A
join [Event] B on A.CaseID = B.CaseID
join Notice C on B.EventID = C.EventID

Please clarify.

+3  A: 

Yes, same queries, different syntax.

The second query is better written as:

SELECT C.* 
FROM [Case] A 
inner join [Event] B on A.CaseID = B.CaseID 
inner join Notice C on B.EventID = C.EventID

The second query using ANSI syntax has some advantages over the first:

  • easy to see when you have a missing ON clause
  • makes it clear what kind of join is being done
  • separates JOIN clause from WHERE clause
RedFilter
Cool.. Thanks guys!
PLus of course it is the more recent syntax, the other is 18 years out of date for reasons you explained above. It is also easier to mainatain especially when needing to add left joins as the old-style syntax does not have good support for left joins (In SQL Server at least). In fact they are deprecated and right now in SQL server that they may return incorrect results as the optimized gets confused as to whether it is a left join or a cross join.
HLGEM
Good points - plus I have a **strong aversion** to the pre-ANSI syntax :)
RedFilter
A: 

When using the comma in the FROM clause, it's the same as the keyword JOIN. Usually, you'd be using INNER JOIN or a form of OUTER JOIN which gives much more efficient results.

Jon