convert subquery to join statement

  • Hi

    Is there a way to get all the items present in one table and not in other table

     

    So if we take query in northwind database in sql server

     

    Select * from orders where orderid not in (select orderid from [order details])

     

    But can above query be converted to join statement?

     

    Thanks

    Amrita

  • Try

    Select * from orders o left join [order details] od

    on o.orderid = od.orderid

    where od.orderid is null

     

  • Select a.*,b.OrderId

    From Orders a(NoLock)

    Left Outer Join [Order Details] b(NoLock) On (a.OrderId = b.OrderId)

    Where b.OrderId Is Null

     

  • Hi venku,

    Just a comment here, y to use NOLOCK hint, i think sql Query Optimizer will choose the best hints so y to override the default behaviour and by the way NOLOCK hint means "dirty reads" are possible.

    Correct me if I'm wrong..

    Regards,

    Dilip

  • Sorry for the use of NoLock, currently I am working on a reporting system and addicted to it.

    Yeah, you can avoid using NoLock.

    Regards,

    Venku

  • hey it's ok buddy..sometimes same happens to me too 😉

  • thnx for the answers

    another question here, which one is better in terms of performance

    ie subquery or left outer join

    i have read subqueries are slow but in this case left outer join is show in execution plan

    ny ideas??

    amrita

     

     

  • Hi Amrita,

    Say, ur orders and orderdetail tables grow tremendously in that case anytimes joins would be better than subqueries..Becoz for a subquery for each outer query it will execute the inner query that many times whether you like it or not, e,g. your Orders table have say 1000 records and OrderDetails have 20000 recs. so for each outer query it will execute the innerquery having 20000 recs...can u just paste the execution plans results out here so that we can have a look?

    Regards,

    Dilip

Viewing 8 posts - 1 through 7 (of 7 total)

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