IN Vs INNER JOIN

  • I want to know which query is more optimized?

    SELECT 1 FROM ABC WHERE Id IN (SELECT Id FROM ABC1)

    ===================================================

    SELECT 1 FROM ABC a JOIN ABC1 b ON a.Id = b.Id

    Thanks

    Puru

  • purushottam2 (9/16/2012)


    I want to know which query is more optimized?

    SELECT 1 FROM ABC WHERE Id IN (SELECT Id FROM ABC1)

    ===================================================

    SELECT 1 FROM ABC a JOIN ABC1 b ON a.Id = b.Id

    Thanks

    Puru

    Actually depends upon the number of records in the table.

    --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
    ๐Ÿ™‚

  • Its also depends on whether the columns you are joining on are indexed.

    Indexing and performance is a large topic in it's own right.

    Too many indexes on a large table may slow down INSERT, UPDATE and DELETE whilst giving improvement to SELECT.

    Where possible, I prefer JOINS from a readability point of view.

    :exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:

  • Robin Sasson (9/17/2012)


    Its also depends on whether the columns you are joining on are indexed.

    Indexing and performance is a large topic in it's own right.

    Too many indexes on a large table may slow down INSERT, UPDATE and DELETE whilst giving improvement to SELECT.

    Where possible, I prefer JOINS from a readability point of view.

    But in many places I have observed that IN beats INNER JOIN in performance.

    --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
    ๐Ÿ™‚

  • Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Agree, Suppose ABC is master tables and ABC1 is child table, in this case IN and Inner Join both will return the same data.

  • An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.

    With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesnโ€™t matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.

    Thanks!

  • Grant Fritchey (9/17/2012)


    Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.

    Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.

    --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
    ๐Ÿ™‚

  • Just found below article from Gail on the same ๐Ÿ™‚

    http://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-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
    ๐Ÿ™‚

  • yuvipoy (9/17/2012)


    An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.

    With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesnโ€™t matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.

    Thanks!

    Copied from Gail's blog. Not exactly the right thing to do unless you also provide the link. It smacks of plagiarism and is most definitely frowned on around here.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • rhythmk (9/17/2012)


    Grant Fritchey (9/17/2012)


    Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.

    Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.

    He didn't say that. It just says, which is optimized.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey (9/17/2012)


    yuvipoy (9/17/2012)


    An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.

    With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesnโ€™t matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.

    Thanks!

    Copied from Gail's blog. Not exactly the right thing to do unless you also provide the link.

    I thought that looked familiar... :hehe:

    yuvipoy, there's a very clear copyright policy on my blog, I'd appreciate if it was followed in the future.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Grant Fritchey (9/17/2012)


    rhythmk (9/17/2012)


    Grant Fritchey (9/17/2012)


    Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.

    Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.

    He didn't say that. It just says, which is optimized.

    Hi Grant,

    Please have a look on the scenario mentioned by OP in subsequent post

    Agree, Suppose ABC is master tables and ABC1 is child table, in this case IN and Inner Join both will return the same data.

    --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
    ๐Ÿ™‚

  • Lots of folks have posted informative replies; but at the end of the day, the only way to know for sure in your own situation is to gather the data.

    set statistics time on

    set statistics io on

    Then run each query and compare the data. Also get the execution plan.

  • 2ndHelping (9/20/2012)


    Lots of folks have posted informative replies; but at the end of the day, the only way to know for sure in your own situation is to gather the data.

    set statistics time on

    set statistics io on

    Then run each query and compare the data. Also get the execution plan.

    Just remember that this will have an impact on the testing, and could skew it.

Viewing 15 posts - 1 through 15 (of 16 total)

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