Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Select 2 tables Expand / Collapse
Author
Message
Posted Monday, September 17, 2012 12:22 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:01 AM
Points: 1,375, Visits: 2,660
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
Post #1359987
Posted Monday, September 17, 2012 12:43 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, July 18, 2014 9:09 AM
Points: 870, Visits: 2,385
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
Post #1359990
Posted Monday, September 17, 2012 12:49 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, July 14, 2014 7:25 AM
Points: 539, Visits: 876
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/
Post #1359992
Posted Monday, September 17, 2012 1:01 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 1:01 AM
Points: 1,375, Visits: 2,660
Thanks
Post #1359999
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse