4

I have a table employee which has a column company which has all null values

Now, I want to use a NOT IN operator for the company name as

select * from employee where employee.company NOT IN ('ABC', 'DEF')

Now technically this should not alter the result since the company column already had null values.

But adding the NOT IN gives 0 rows.

Is this due to the fact that employee.company column has NULL values?

2
  • 2
    If I'm correct, you'll need to use 'NOT EXISTS' instead of NOT IN. Jun 18, 2013 at 13:58
  • 1
    FYI - Null is not a value like 'ABC', 001 etc.
    – IT ppl
    Jun 18, 2013 at 14:02

2 Answers 2

10

Try this way:

select * 
from employee E
where (E.company NOT IN ('ABC', 'DEF')
       or E.company is null)
4

SQL uses three valued logic: true, false, and unknown. A comparison with null results in unknown, which is not true.

So a where clause like:

where null in (1,2)

Will not return any rows.

9
  • 1
    "unknown, which is not true" - but "not true" IS false, by definition. :)
    – mbeckish
    Jun 18, 2013 at 14:06
  • @mbeckish, unknown is by definition not true. is it by definition not false either. The headache disappears once you understand that this should not be understood under bool's laws because there are 3 bits, not 2.
    – Sebas
    Jun 18, 2013 at 14:10
  • 1
    @mbeckish I think what Andomar meant was not unknown == !true but unknown != true. Pure grammar, nothing to do with logic in this case.
    – Sebas
    Jun 18, 2013 at 14:16
  • 1
    @Sebas - I know, that's why I put the smiley face on my original comment. Just pointing out that it's very tricky to explain these finer points using plain language, especially when the community includes people for whom English is not their first language.
    – mbeckish
    Jun 18, 2013 at 14:20
  • 4
    @mbeckish: The answer states that unknown is not true. The answer does not state that unknown is not true :)
    – Andomar
    Jun 18, 2013 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.