NOT IN

  • Comments posted to this topic are about the item NOT IN

  • Good question & new piece of information for me, so thanx a lot.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • This was removed by the editor as SPAM

  • Also a good illustration of why NOT IN can be very risky. Using NOT EXISTS instead gives the result that would probably be intended in this scenario:

    SELECT Value

    FROM #tmpNewValue new

    WHERE NOT EXISTS (

    SELECT 1 FROM #tmpOldValue old

    WHERE old.Value = new.Value);

  • Thanks for the question.

    Good day to all.

    ---------------
    Mel. 😎

  • Agree.

    Richard Warr (7/28/2015)


    Also a good illustration of why NOT IN can be very risky. Using NOT EXISTS instead gives the result that would probably be intended in this scenario:

    SELECT Value

    FROM #tmpNewValue new

    WHERE NOT EXISTS (

    SELECT 1 FROM #tmpOldValue old

    WHERE old.Value = new.Value);

    ---------------
    Mel. 😎

  • Here's a nice, straightforward -- but perhaps fuller -- explanation of what's going on here if you're still puzzled:

    https://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/[/url]

    EDIT: URL corrected for the web-friendly version of the page instead of print preview.

    Rich

  • This behavior is a lot easier to understand if you think about NOT IN as a series of 'not equal' statements logically ANDed together:

    SELECT Value

    FROM #tmpNewValue

    WHERE Value != 3

    AND Value != 7

    AND Value != Null

    AND Value != 6

    Since any evaluation with Null is undetermined (which gets logically interpreted as false), the whole expression evaluates to false and no rows are returned.

  • Alternative solutions:

    SELECT Value

    FROM #tmpNewValue

    WHERE Value not in(

    SELECT Value FROM #tmpOldValue where Value is not null

    )

    SELECT Value

    FROM #tmpNewValue

    WHERE Value <> all(

    SELECT Value FROM #tmpOldValue where Value is not null

    )

    Gerald Britton, Pluralsight courses

  • NULL can cause grief if you're not careful.

  • If the question could have been asked to check the returned value:

    SELECT Value

    FROM #tmpOldValue

    WHERE Value NOT IN (SELECT Value FROM #tmpNewValue)

    The answer would have been := 6

    Thanks.

  • SQL-DBA-01 (7/28/2015)


    If the question could have been asked to check the returned value:

    SELECT Value

    FROM #tmpOldValue

    WHERE Value NOT IN (SELECT Value FROM #tmpNewValue)

    The answer would have been := 6

    Please, lets not start discussing all the possible answers if the question was different.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Easy... Thanks, Sergey!

  • This was a good question that makes one really think about null value.

  • sestell1 (7/28/2015)


    This behavior is a lot easier to understand if you think about NOT IN as a series of 'not equal' statements logically ANDed together:

    Perfect way to restate the original point.

    Thanks,
    Naveen.
    Every thought is a cause and every condition an effect

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

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