Using the correct, or preferable, not equal operator in MySQL

Rob Van Dam picture Rob Van Dam · Jan 14, 2010 · Viewed 37k times · Source

Which of the two (semantically equivalent) ways is preferable to test for inequality?

  1. 'foo' != 'bar' (exclamation mark and equals sign)
  2. 'foo' <> 'bar' (less than and greater than chevron symbols together)

The MySQL documentation clearly indicates that there is no difference between them and yet some people seem to be attached to only doing it one way or the other. Maybe this is just another pointless vi vs. emacs debate but when other people are reading your code (and therefore your queries), it's useful to maintain some consistency.

<> looks a lot like <=> which is a very underused operator but could perhaps lead to confusion at a quick glance since the two are nearly opposite (except for the obvious NULL cases).

Answer

jspcal picture jspcal · Jan 14, 2010

<> should be preferred, all things being equal, since it accords with the sql standard and is technically more portable...

!= is non-standard, but most db's implement it.

sql:2008 grammar:

<not equals operator> ::=
  <>