Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2010
    Posts
    5

    Unanswered: Subquery or Inner Join?

    When do you want to use a subquery, ie:

    SELECT SUM(Sales) FROM Store_Information
    WHERE Store_name IN
    (SELECT store_name FROM Geography
    WHERE region_name = 'West')

    OR

    when do you want to use a INNER JOIN?

    Thanks.
    Last edited by smalvarez; 09-17-10 at 23:22.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    there are some cases where the subquery is not as efficient as a join(typically when it is a correlated subquery)

    however, i think this is a personal preference, and i think that the join is a lot easier to understand
    Code:
    SELECT SUM(Sales) 
      FROM Geography
    INNER
      JOIN Store_Information
        ON Store_Information.Store_name = Geography.store_name  
     WHERE Geography.region_name = 'West'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2010
    Posts
    6

    Subquery or inner jion??

    Though it depends on your wish,, one of the thing u should check is the execution plan for each type such as sub query or inner join.... Both type may may have different cpu cost or I/O Cost

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •