Is using the not equal <> sign in a SQL statement poor programming practice?
I have three values in a table column type. The values can be 0, 1, or 2. Which SQL statement is better?
1) WHERE type <> 2
2) WHERE type in (0,1)
Is using the not equal <> sign in a SQL statement poor programming practice?
I have three values in a table column type. The values can be 0, 1, or 2. Which SQL statement is better?
1) WHERE type <> 2
2) WHERE type in (0,1)
Neither is better than the other. Once you introduce a new type you might have to change one statement or the other.
An example: Let's say the type means "who pays".
So our query means "we don't pay". When introducing
then you'd have to change
WHERE type <> 2
to
WHERE type NOT IN (2,3)
When introducing
then you'd have to change the other query from
WHERE type IN (0,1)
to
WHERE type IN (0,1,3)
If you want to make it super-correct, you have a lookup table:
type meaning wepay 0 supplier pays false 1 client pays false 2 our selling department pays true
And the query:
WHERE type NOT IN (select type from types where wepay = true)
or
WHERE type IN (select type from types where wepay = false)
Again, it doesn't matter which of the two options to choose.
... IN ( SELECT... significantly slower? just asking