SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Question about JOINs and table order
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Xercister
Starting Member

USA
6 Posts

Posted - 03/19/2013 :  22:08:58  Show Profile  Reply with Quote
So I've been reading up on JOINS and for the most part I get it. However I do have a few questions that I am not finding answers to.

1. If you have 3 tables in your database.

Table 1, Table 2, Table 3

Table 1 relates to Table 2
Table 1 relates to Table 3
Table 2 does not relate to Table 3

If you wanted to use these tables together can you do something like this?

SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T2.xx

If not, why not?

2. Order of the ON clause in the JOIN syntax.

If you're joining Table 1 and Table 2 I would assume you would use this.

FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T2.xx = T1.xx.

Does it matter what order the fields are that you're joining them on?

Is that the same as this?

FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx

Thanks for the help in advance!

chadmat
The Chadinator

USA
1974 Posts

Posted - 03/20/2013 :  00:29:31  Show Profile  Visit chadmat's Homepage  Reply with Quote
1. Yes
2. Yes you can, order doesn't matter.

-Chad
Go to Top of Page

UnemployedInOz
Yak Posting Veteran

Australia
54 Posts

Posted - 03/21/2013 :  01:59:59  Show Profile  Reply with Quote
This doesnt look right
SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T2.xx

It should be

SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T3.xx -- Should be T3.xx not T2.xx
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 03/21/2013 :  02:10:55  Show Profile  Visit chadmat's Homepage  Reply with Quote
quote:
Originally posted by UnemployedInOz

This doesnt look right
SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T2.xx

It should be

SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T3.xx -- Should be T3.xx not T2.xx



Yes, you are correct, I missed that.

-Chad
Go to Top of Page

Xercister
Starting Member

USA
6 Posts

Posted - 03/21/2013 :  07:37:56  Show Profile  Reply with Quote
quote:
Originally posted by UnemployedInOz

This doesnt look right
SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T2.xx

It should be

SELECT *
FROM Table 1 AS T1
JOIN Table 2 AS T2 ON T1.xx = T2.xx
JOIN TABLE 3 AS T3 ON T1.xx = T3.xx -- Should be T3.xx not T2.xx



Ah yea, gotta love those typos.. Thanks for catching that.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.25 seconds. Powered By: Snitz Forums 2000