Home All Groups Group Topic Archive Search About

Left Outer Join and Where Clause

Author
10 Dec 2005 10:29 AM
Kiran
Hi,

I have two tables Table1 and Table2.

I need to join Table2 on Table1_SID to get name for SID Column of Table1.

But I need all rows of Table1.

I can use a Left Outer Join to achieve this.

Now I have a problem. There are rows in table2 and table1 where
Table1_SID and SID have -1 value in it.

Don't want to join these rows, join rows where Table1_SID>0 and need to
get all the rows from Table1.

Hope you guys understand what I need.


Thanks
Kiran

Author
10 Dec 2005 12:28 PM
Tom Moreau
Try:

select
    *
from
    Table1 t1
left join
    Table t2 on t2.Table1_SID = t1.SID
                and t2.Table1_SID > 0

--
    Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada  t**@cips.ca
www.pinpub.com

Show quote
"Kiran" <some***@somewhere.com> wrote in message
news:%23cm8MSX$FHA.1028@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> I have two tables Table1 and Table2.
>
> I need to join Table2 on Table1_SID to get name for SID Column of Table1.
>
> But I need all rows of Table1.
>
> I can use a Left Outer Join to achieve this.
>
> Now I have a problem. There are rows in table2 and table1 where Table1_SID
> and SID have -1 value in it.
>
> Don't want to join these rows, join rows where Table1_SID>0 and need to
> get all the rows from Table1.
>
> Hope you guys understand what I need.
>
>
> Thanks
> Kiran
Author
12 Dec 2005 5:17 AM
Kiran
Tom Moreau wrote:
> Try:
>
> select
>     *
> from
>     Table1 t1
> left join
>     Table t2 on t2.Table1_SID = t1.SID
>                 and t2.Table1_SID > 0
>
Hi Tom,

There is a problem with the above query, it doesn't return me the
records of Table1 with SID=-1.

Can you also explain me why doesn't it return the records of Table1 with
SID=-1 when I have the where condition for table2.


Thanks
Kiran
Author
12 Dec 2005 1:08 PM
Tom Moreau
OK, I think I'm understanding what you want.  How about:

select
    *
from
    Table1 t1
left join
(
    select
        *
    from
        Table
    where
        Table1_SID > 0
) t2 on t2.Table1_SID = t1.SID



--
    Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada  t**@cips.ca
www.pinpub.com

Show quote
"Kiran" <some***@somewhere.com> wrote in message
news:e4r2ktt$FHA.356@TK2MSFTNGP12.phx.gbl...
> Tom Moreau wrote:
>> Try:
>>
>> select
>>     *
>> from
>>     Table1 t1
>> left join
>>     Table t2 on t2.Table1_SID = t1.SID
>>                 and t2.Table1_SID > 0
>>
> Hi Tom,
>
> There is a problem with the above query, it doesn't return me the records
> of Table1 with SID=-1.
>
> Can you also explain me why doesn't it return the records of Table1 with
> SID=-1 when I have the where condition for table2.
>
>
> Thanks
> Kiran
Author
12 Dec 2005 2:11 PM
Kiran
Tom Moreau wrote:
Show quote
> OK, I think I'm understanding what you want.  How about:
>
> select
>     *
> from
>     Table1 t1
> left join
> (
>     select
>         *
>     from
>         Table
>     where
>         Table1_SID > 0
> ) t2 on t2.Table1_SID = t1.SID
>
>
>
Hi Tom,

thanks a lot, it worked.

does it has any performance issues.


Thanks
Kiran
Author
12 Dec 2005 3:06 PM
Tom Moreau
Performance would depend on how you have indexed your tables.

--
    Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada  t**@cips.ca
www.pinpub.com

Show quote
"Kiran" <some***@somewhere.com> wrote in message
news:us7EoXy$FHA.356@TK2MSFTNGP12.phx.gbl...
> Tom Moreau wrote:
>> OK, I think I'm understanding what you want.  How about:
>>
>> select
>>     *
>> from
>>     Table1 t1
>> left join
>> (
>>     select
>>         *
>>     from
>>         Table
>>     where
>>         Table1_SID > 0
>> ) t2 on t2.Table1_SID = t1.SID
>>
>>
>>
> Hi Tom,
>
> thanks a lot, it worked.
>
> does it has any performance issues.
>
>
> Thanks
> Kiran
Author
12 Dec 2005 6:12 PM
Kiran
Tom Moreau wrote:
> Performance would depend on how you have indexed your tables.
>
Hi Tom,

Not that, we usually join tables like

select * from table1 left outer join table2 on table1.SID=table2.table1_SID.

In my case, since my requirement was different, I am using it like this

select
      *
from
     Table1 t1
  left join
  (
      select
         *
      from
          Table
      where
          Table1_SID > 0
  ) t2 on t2.Table1_SID = t1.SID


so is there any performance issue between these two queries


Thanks
Kiran
Author
12 Dec 2005 6:25 PM
Tom Moreau
They are two different queries.  I don't understand your question.

--
    Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON   Canada  t**@cips.ca
www.pinpub.com

Show quote
"Kiran" <some***@somewhere.com> wrote in message
news:OHrbOe0$FHA.2784@tk2msftngp13.phx.gbl...
> Tom Moreau wrote:
>> Performance would depend on how you have indexed your tables.
>>
> Hi Tom,
>
> Not that, we usually join tables like
>
> select * from table1 left outer join table2 on
> table1.SID=table2.table1_SID.
>
> In my case, since my requirement was different, I am using it like this
>
> select
>      *
> from
>     Table1 t1
>  left join
>  (
>      select
>         *
>      from
>          Table
>      where
>          Table1_SID > 0
>  ) t2 on t2.Table1_SID = t1.SID
>
>
> so is there any performance issue between these two queries
>
>
> Thanks
> Kiran
Author
12 Dec 2005 6:42 PM
Kiran
Tom Moreau wrote:
> They are two different queries.  I don't understand your question.
>

It's ok Tom, thanks a ton for the help.

Just wanted to know is there any decrease in performance due to the way
I join.

Thanks
Kiran
Author
13 Dec 2005 11:53 PM
Hugo Kornelis
On Mon, 12 Dec 2005 18:42:01 +0000, Kiran wrote:

>Tom Moreau wrote:
>> They are two different queries.  I don't understand your question.
>>
>
>It's ok Tom, thanks a ton for the help.
>
>Just wanted to know is there any decrease in performance due to the way
>I join.

Hi Kiran,

Comparing the performance of two queries that produce different results
is irrelevant. Unless you have a boss who doesn't care which question
you answer, as long as you answer it quick <g>. That's why I can't
comment on the relative performance of Tom's query vs your original
query.

I can give a generic answer, though. Using a different way to join while
keeping the meaning (and hence the result) of the query the same will
usually (but not always) not change performance. The optimizer will
consider alternative ways to reach the required result anyway. But in
complicated queries, there are cases where rewriting the query enabled
the optimizer to find a better plan that it couldn't find when executing
the original query.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

AddThis Social Bookmark Button