I have seen SQL
that uses both !=
and <>
for not equal. What is the preferred syntax and why?
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.