tags:

views:

51

answers:

4

I've saw a join just like this:

Select <blablabla>
from 
  TableA TA
  Inner join TableB TB on Ta.Id = Tb.Id
  Inner join TableC TC on Tc.Id = Tb.Id and Ta.OtheriD = Tc.OtherColumn

But what's the point (end effect) of that second join clause?
What the implications when an outer join clause is used?
And, more important, what is the best to rewrite it in a way that is easy to understand what it's trying to join?
And, more important, what is the best way to rewrite it to get rid of the construction and mantain the correctness of the query.
I don't specify the RDBMS, because it's a more generic question, but for those curious (since people always ask): it's SQL Server 2005.

EDIT: It's just a made up example (since I would have to dig the original source - which I don't have access anymore). I found the original join clause on a 10 join SELECT command.

+3  A: 

It simply means you have an extra restriction on the intersection between tablea and tablec.

Because we know Ta.Id = Tb.Id, Tc.Id = Tb.Id is the same as Tc.Id = Ta.Id. Inner joins are associative. So it makes more sense like this so each join is between 2 tables only

Select <blablabla>
from 
  TableB TB 
  Inner join
  TableA TA on Tb.Id = Ta.Id   --a and b intersection
  Inner join
  TableC TC on Ta.Id = Tc.Id and Ta.OtheriD = Tc.Column   --a and c intersection
gbn
Seems I have to reorder the joins to be able to eliminate the two table condition in the 'ON' clause. hmmmm
Fabricio Araujo
@Fabricio Araujo: it's the same JOIN and will give the same result because "INNER JOIN" is associative and commutative. I hoped to clarify what is going on for you.
gbn
Read the edit. It's MADE UP, just to example. It could be an OUTER join. I just used an inner join - I just want to understand the effect and the best (less dangerous) way to get rid of this type of construction.
Fabricio Araujo
@Fabricio Araujo: An OUTER JOIN is different and order does matter. You've had 3 answers all similar: what are you really asking?
gbn
Sorry, I agree that I was not clear. I rewrote part of the question and expect this time I got it right.
Fabricio Araujo
+1  A: 

The user may want to furthur filter the set of rows which are included in the Join set...

Charles Bretana
A: 

The point of the second join is to further limit your result set based on the contents of TableC. The first join gives you ONLY records that exist in TA and TB. The second join gives you ONLY results from the first join that also exist in TC.

JNK
+2  A: 

Your Q : But what's the point (end effect) of that second join clause?

Effectively filters rows...you could move the second half of the on statement into the where clause if you really want, only really effects readability. gbn's answer looks good for this 3 table example,but to expand on it...sometimes a rewrite like this isn't possible. I have seen an occasion where 2 different systems (one oracle 8i and one SQL server 2000) had their databases joined together. A 3 part key was identified as being required to make the records unique in both systems, but each component of the 3 part key was held in different tables...the final result had a few joins like that.

Functionally...I'm not sure if there's a difference really. Unless I'm completely off, readability seems to be the biggest difference.

Your Second Q: What the implications when an outer join clause is used?

You'll potentially get a bunch of nulls (pending how you setup the outer join) while the inner join would have dropped them. Be careful though...inner joins is associative...as gbn put it: An OUTER JOIN is different and order does matter

M.E.