I have seen SQL
that uses both !=
and <>
for not equal. What is the preferred syntax and why?
Most databases support !=
(popular programming languages) and <>
(ANSI).
Databases that support both !=
and <>
:
Databases that support the ANSI standard operator, exclusively:
!=
, despite being non-ANSI, is more in the true spirit of SQL as a readable language. It screams not equal.
<>
says it's to me (less than, greater than) which is just weird. I know the intention is that it's either less than or greater than hence not equal, but that's a really complicated way of saying something really simple.
I've just had to take some long SQL queries and place them lovingly into an XML file for a whole bunch of stupid reasons I won't go into.
Suffice to say XML is not down with <>
at all and I had to change them to !=
and check myself before I riggedy wrecked myself.
They are both accepted in T-SQL. However, it seems that using <>
works a lot faster than !=
. I just ran a complex query that was using !=
, and it took about 16 seconds on average to run. I changed those to <>
and the query now takes about 4 seconds on average to run. That's a huge improvement!
<>
is the valid SQL according to the SQL-92 standard.
http://msdn.microsoft.com/en-us/library/aa276846(SQL.80).aspx
You can use whichever you like in T-SQL. The documentation says they both function the same way. I prefer !=
, because it reads "not equal" to my (C/C++/C# based) mind, but database gurus seem to prefer <>
.
The ANSI SQL Standard defines <>
as the "not equal to" operator,
http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt (5.2 <token> and <separator>
)
There is no !=
operator according to the ANSI/SQL 92 standard.