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
 Joining Two Select statements

Author  Topic 

wisdomtree
Starting Member

1 Post

Posted - 2008-10-20 : 04:11:15
Hi I have come across a similar query which was written by my previous db developer. can any one explain how does this work and when should i use such query

select * from
(select a.x1,a.x2,.. from t1) a
inner join
(select b.x1,b,x2 from t2) b
on (a.x1=b.x2)

i am not sure whether the above code i wrote is right but hope it gives you an idea about what i am asking.
Please help me out by giving some example so that i will be able to understand

rgs
bhanu

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-20 : 04:13:39
its right. only problem is it wont need alises inside as aliases are defined for derived tables and wont be available inside

select * from
(select x1,x2,.. from t1) a
inner join
(select x1,x2 from t2) b
on (a.x1=b.x2)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-20 : 04:17:10
or

select a.columns,b.columns from t1 as a inner join t2 as b on (a.x1=b.x2)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-20 : 04:47:33
quote:
Originally posted by madhivanan

or

select a.columns,b.columns from t1 as a inner join t2 as b on (a.x1=b.x2)

Madhivanan

Failing to plan is Planning to fail


this is prefered way to do unless you dont have any specific calculations/dervivations to done on a any table column before joining.
Go to Top of Page
   

- Advertisement -