I have seen SQL
that uses both !=
and <>
for not equal. What is the preferred syntax and why?
It seems that Microsoft themselves prefer <>
to !=
as evidenced in their table constraints. I personally prefer using !=
because I clearly read that as "not equal", but if you enter [field1 != field2]
and save it as a constrait, the next time you query it, it will show up as [field1 <> field2]
. This says to me that the correct way to do it is <>
.
Although they function the same way, !=
means exactly "not equal to", while <>
means greater than and less than the value stored.
Consider >=
or <=
, and this will make sense when factoring in your indexes to queries... <>
will run faster in some cases (with the right index), but in some other cases (index free) they will run just the same.
This also depends on how your databases system reads the values !=
and <>
. The database provider may just shortcut it and make them function the same, so there isn't any benefit either way.PostgreSQL and SQL Server do not shortcut this; it is read as it appears above.