Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL WHERE condition is not equal to?

Is it possible to negate a where clause?

e.g.

DELETE * FROM table WHERE id != 2; 
like image 999
Frank Vilea Avatar asked May 27 '11 19:05

Frank Vilea


People also ask

How do you do a not equal condition in SQL?

The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 !=

Can we use not equal to in where clause?

The SQL not equal operator is <>. You should specify this in a WHERE statement. This lets you select rows where a particular column's contents is not equal to the value you have specified. You can also use !=

Can you use != In SQL?

There is no != operator according to the ANSI/SQL 92 standard.

Is != And <> same in SQL?

Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.


1 Answers

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.

like image 75
Praveen Lobo Avatar answered Sep 20 '22 19:09

Praveen Lobo