Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joins

Author  Topic 

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2009-03-16 : 13:08:57
I need to extract data from three tables but think I have the join wrong. Here is what I have:

from (hosp_nmrc LEFT JOIN hosp_rpt on hosp_rpt.RPT_REC_NUM = hosp_nmrc.RPT_REC_NUM) and (state_codes RIGHT JOIN hosp_rpt on state_codes.Ssa_State_Cd = hosp_nmrc.state_code)

How are joins on multiple tables coded?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-16 : 13:13:06
from (hosp_nmrc LEFT JOIN hosp_rpt on hosp_rpt.RPT_REC_NUM = hosp_nmrc.RPT_REC_NUM)
LEFT JOIN state_codes on state_codes.Ssa_State_Cd = hosp_nmrc.state_code
Go to Top of Page

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2009-03-16 : 13:43:17
quote:
Originally posted by visakh16

from (hosp_nmrc LEFT JOIN hosp_rpt on hosp_rpt.RPT_REC_NUM = hosp_nmrc.RPT_REC_NUM)
LEFT JOIN state_codes on state_codes.Ssa_State_Cd = hosp_nmrc.state_code



Thank you, Vikash!

If I want all of the records in the state codes table, should I use a left join or a right join? Why?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-16 : 13:46:45
if you want all records in state code table regardless of other condition, you ned like this


from state_codes LEFT JOIN hosp_nmrc on state_codes.Ssa_State_Cd = hosp_nmrc.state_code
LEFT JOIN hosp_rpt on hosp_rpt.RPT_REC_NUM = hosp_nmrc.RPT_REC_NUM
Go to Top of Page

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2009-03-16 : 13:53:53
quote:
Originally posted by visakh16

if you want all records in state code table regardless of other condition, you ned like this


from state_codes LEFT JOIN hosp_nmrc on state_codes.Ssa_State_Cd = hosp_nmrc.state_code
LEFT JOIN hosp_rpt on hosp_rpt.RPT_REC_NUM = hosp_nmrc.RPT_REC_NUM



Why does that work? Sorry for the all the questions! I am new to SQL!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-16 : 13:59:01
the table from which you want should be first in query (base table) folowed by left joins. this will make sure all records from left potion of join are included (base table) and matching values if any will be returned from right side of joins. if no matches, then NULL will be returned. suggest you to read about left,right,inner...joins from books online to understand the complete difference
Go to Top of Page

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2009-03-16 : 14:04:55
quote:
Originally posted by visakh16

the table from which you want should be first in query (base table) folowed by left joins. this will make sure all records from left potion of join are included (base table) and matching values if any will be returned from right side of joins. if no matches, then NULL will be returned. suggest you to read about left,right,inner...joins from books online to understand the complete difference



Great, thank you for the explanation. One other thing I am still getting used to is using on-line books as a resource!

Thanks again,

JB
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-16 : 14:08:48
have you not installed sql server books online?
Go to Top of Page

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2009-03-16 : 15:15:36
quote:
Originally posted by visakh16

have you not installed sql server books online?



No, is there something to install? I just look at it online by going to:

http://msdn.microsoft.com/en-us/library/ms130214.aspx

JB
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-16 : 17:14:56
You get the same along with the installation. A help menu which is pretty much the same as what you have online. You might have not installed it.
Go to Top of Page
   

- Advertisement -