What is the difference between NOT
and !=
operators in SQL? I can’t understand the difference. I guess they are same.
Advertisement
Answer
NOT
negates the following condition so it can be used with various operators. !=
is the non-standard alternative for the <>
operator which means “not equal”.
e.g.
NOT (a LIKE 'foo%') NOT ( (a,b) OVERLAPS (x,y) ) NOT (a BETWEEN x AND y) NOT (a IS NULL)
Except for the overlaps
operator above could also be written as:
a NOT LIKE 'foo%' a NOT BETWEEN x AND y a IS NOT NULL
In some situations it might be easier to understand to negate a complete expression rather then rewriting it to mean the opposite.
NOT
can however be used with <>
– but that wouldn’t make much sense though: NOT (a <> b)
is the same as a = b
. Similarly you could use NOT to negate the equality operator NOT (a = b)
is the same as a <> b