How "NOT IN" works here?

Last post 07-30-2008, 10:04 PM by khtan. 1 replies.
Sort Posts: Previous Next
  •  07-15-2008, 12:38 AM Post number 62453

    How "NOT IN" works here?

    select * from (
        select 1 as a union
        select 2 union
        select 3 union
        select 4 union
        select 5
    )tb where tb.a not in (1,2,null)


    Please can any one tell me why is the above query not returning any row.
    As per my expectation it should return 3 row.
    Problem here is that when i use  null it not works, but why?
    if I remove null it works fine.
    How NOT IN works ?
    Thanks in advance

    I am using sql server 2005
  •  07-30-2008, 10:04 PM Post number 65195 in reply to post number 62453

    • khtan is not online. Last active: 07-30-2008, 10:07 PM khtan
    • Top 150 Contributor
    • Joined on 01-15-2007
    • Level 1: Deep thought

    Re: How "NOT IN" works here?

    what will this gives you ?
    select * from
    (
        select 1 as a union
        select 2 union
        select 3 union
        select 4 union
        select 5
    )tb where tb.a not in (1,2)

    3,4,5 right ? NOT IN basically can be interpret as
    WHERE tb.a <> 1
    AND     tb.a  <> 2

    so with tb.a not in (1, 2, null)
    it will be
    WHERE tb.a <> 1
    AND     tb.a  <> 2
    AND    tb.a <> NULL

    Remember that when comparing with NULL, it will always be FALSE
View as RSS news feed in XML