SQL WHERE condition is not equal to?

Frank Vilea picture Frank Vilea · May 27, 2011 · Viewed 254.4k times · Source

Is it possible to negate a where clause?

e.g.

DELETE * FROM table WHERE id != 2;

Answer

Praveen Lobo picture Praveen Lobo · May 27, 2011

You can do like this

DELETE FROM table WHERE id NOT IN ( 2 )

OR

DELETE FROM table WHERE id <>  2 

As @Frank Schmitt noted, you might want to be careful about the NULL values too. If you want to delete everything which is not 2(including the NULLs) then add OR id IS NULL to the WHERE clause.