Re: Null and not equal in Join Condition

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 08/19/04


Date: Thu, 19 Aug 2004 21:48:29 +0200

On Thu, 19 Aug 2004 12:03:00 -0700, Robert Taylor wrote:

>I know that Null does not equal anything. Does that mean that I cannot
>do a join where column A has values and column B may have Nulls and
>legitimate values like this:
>
>where
>join on table1.column1=table2.column1
>AND
>***table1.columnA != table2.columnB ***
>
>Any thoughts?
>
>Thanks,
>
>Robert

Hi Robert,

What you use above is invalid syntax, regardless of the data containing
NULLS or not.

However, you can write

SELECT some columns
FROM table1
INNER JOIN table2
      ON table1.column1 = table2.column1
WHERE table1.columnA != table2.columnB

But you should be aware of how NULLS behave in logical expressions. You
must familiarize yourself with three-valued logic. Unlike the better known
boolean logic, three-valued logic has "unknown" in addition to "true" and
"false". Any comparison to a NULL value will always be "unknown". It makes
sense, actually: is 17 equal to an value that is not available to the
database? of course, you can't know. But neither can you know if 17 is NOT
equal to a value that's not in the database. And you also can't tell if
two values that are not known to a database are equal to each other, so it
actually makes sense that NULL = NULL and NULL <> NULL both evaluate to
unknown, not to true and false respectively.

Three-valued logic also has impact on how the NOT, AND and OR operators
work. But you can easily work that out for yourself: just take three
propositions, one of which you know to be true (eg: I am Robert), one of
which is false (eg: the Earth is flat) and one of which is unknown to you
(eg: President Bush is wearing pink underwear) - at least, I HOPE that one
is unknown to you :-)
You can work out the results of NOT, AND and OR by combining these
propositions with NOT, AND and OR and answering the question yourself: is
it true that the Earth is flat OR President Bush is wearing pink
underwear? Answer: you don't know, so "false" OR "unknown" = "unknown".

The final thing to know is that SQL doesn't give rows the benefit of the
doubt. A row will only be returned if the complete WHERE clause for that
row evaluates to "true" - if it's "false" or "unknown", it doesn't get
into the result.

Conclusion: the above query is correct; if there are no other conditions,
no rows from table2 with columnB equal to NULL will be in the result. If
that is not what you try to achieve, then the query is wrong for your
problem. But that's another issue.

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: Does entity integrity imply entity identity?
    ... If the requirement is to record in the database as much information as is ... then how can the fact that a value is at present unknown be ... on basic theory that cover the subject of nulls and alternative ... Did you search the cdt group archives for related posts? ...
    (comp.databases.theory)
  • Re: So whats null then if its not nothing?
    ... two nulls are not distinct; ... it means effectively that unknown is treated as ... constraints leads to tuples being rejected if false; ... The SQL unique constraint does not consider two ...
    (comp.databases.theory)
  • Re: So whats null then if its not nothing?
    ... >> According to the 3vl rules, whether two nulls are distinct or equal is ... it means effectively that unknown is treated as ... Clearly, you have a duplicate. ... the result is unknown and therefore the constraint is not violated. ...
    (comp.databases.theory)
  • Re: Why does 1:3 relationsihp require another table?
    ... What is meaning for NULL in the ... > column for employees who are non-commissioned? ... Really the set of NULLs does represent an empty set. ... shipdate for a shipment could be unknown. ...
    (microsoft.public.sqlserver.programming)
  • Re: Why does 1:3 relationsihp require another table?
    ... practically difficult if not impossible and that is why NULLs or any such ... If representation of unknown value is the idea behind the ... implementation of NULLs in SQL, then SQL avoids far too many kinds of ... By assuming support for just one kind of missing information (Unknown value ...
    (microsoft.public.sqlserver.programming)