Select 2 tables

  • 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

  • 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

  • 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

    https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
    🙂

  • Thanks

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply