views:

160

answers:

6

Perhaps this is a stupid question but we've had some debate recently.

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

  1. 'foo' != 'bar'
  2. 'foo' <> 'bar'

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), its useful to maintain some consistency.

One interesting element to the debate is that <> 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).

+2  A: 

DBA's generally like <> and programmers like !=. Just an observation :-)

JonH
I would like to think that DBA's might be programmers too? X-)
astander
+8  A: 

<> 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> ::=
  <>
jspcal
Agreed, http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt says `<not equals operator> ::= <>`
Peter Lang
+2  A: 

They are the same, it is purely preference.

This should give you a good idea

Operators

!= (Not Equal To) Not equal to (not SQL-92 standard)

<> (Not Equal To) Not equal to

astander
+3  A: 

<> is the only one in the SQL-92 standard.

Joe
A: 

The <> operator is the one that is in the SQL standard, so most people that know SQL will be accustomed to it, or at least aware of it. I myself wasn't even aware of that the != operator was also available in some SQL dialects until recently.

As you noticed, people tend to use only one or the other, and that is a good approach, at least on a project-by-project basis. Whichever you choose to use, be consistent.

Guffa
A: 

It's obvious.

The ! character is on the North West corner of US keyboards.

Microsoft headquarters are in the North West corner of the US.

So. <> is a nod to Microsoft.

!= is a rejection of Microsoft.

It's a secret political code.

S.Lott
So if we negate the not-equals comparison, are we supporting Microsoft? Or are we rejecting our own disapproval? :)
Bill Karwin