SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select 2 tables


Select 2 tables

Author
Message
SQLSACT
SQLSACT
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2106 Visits: 2931
Hi All

Consider the following 2 tables

create table T1
(Col1 int, Col2 int)
go
create table T2
(Col3 int, Col4 int)



And the following query
select Col1, Col4 from T1,T2



What is actually happening when the select statement is executed? Is it an inner join or left join?

Thanks
Jason-299789
Jason-299789
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2099 Visits: 3232
You are doing a Cross join so what will happen is that you will get T1 rowcount * t2 Count

Eg if you had 2 rows in T1 and 4 in T2 you would get 8 rows returned with data something like this returned

T1 T2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4

To turn it into an Inner join you need to do

Select Col1, Col4
From T1,T2
Where
T1.Col1=T2.Col3

Or a Left Join then this

Select Col1, Col4
From T1,T2
Where
T1.Col1*=T2.Col3

for a right outer the clause is =*, Unfortunately I cant remember the syntax for a Full outer.

I believe this style of left/right outer join syntax (*= etc) is being depricated over the next couple of releases of SQL server, in 2008 there is a server setting to allow the syntax, im not sure about SQL 2012 though

_________________________________________________________________________
SSC Guide to Posting and Best Practices
rhythmk
rhythmk
SSC Eights!
SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)SSC Eights! (916 reputation)

Group: General Forum Members
Points: 916 Visits: 1078
SQLSACT (9/17/2012)
Hi All

Consider the following 2 tables

create table T1
(Col1 int, Col2 int)
go
create table T2
(Col3 int, Col4 int)



And the following query
select Col1, Col4 from T1,T2



What is actually happening when the select statement is executed? Is it an inner join or left join?

Thanks


Neither inner nor left join. It would be a cross join.

--rhythmk
------------------------------------------------------------------
To post your question use below link

http://www.sqlservercentral.com/articles/Best+Practices/61537/
:-)
SQLSACT
SQLSACT
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2106 Visits: 2931
Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search