In the WHERE
clause of a SQL query I would expect these two conditions to have the same behavior:
NOT (a=1 AND b=1)
vs
a<>1 AND b<>1
The first condition behaves as expected, and while I epxect the second condition to do the same thing, it does not.
This is very basic stuff, but ashamedly I can't see what I'm doing wrong.
Best Answer
They are not equivalent.
is equivalent with:
This equivalence is known as
De Morgan's Law
. See for example:https://en.wikipedia.org/wiki/De_Morgan%27s_laws
A nice technique for proving/disproving equivalences for boolean algebra expressions is to use a cte for the domains, and compare the expressions side by side:
Edit: Since DB2 does not support Boolean datatype I expanded the example at:
http://sqlfiddle.com/#!15/25e1a/19
The rewritten query looks like:
The result of the query is:
As shown exp1 and exp2 are equivalent.