NOT operator in sql

NOT operator in sql

Blog Bustle 16/03/2020

We use NOT operator in sql to filter sql query result with where clause. We can use multiple operators to join conditions in single sql query. We can use sql NOT operator with select, update and delete query.

Syntax

The basic syntax of NOT operator in sql

1. NOT operator in sql select query

SELECT column_1, column_2, column_3, ....
FROM table_name
WHERE NOT condition;

2. NOT operator in sql update query

UPDATE table_name
SET column_1 = value_1,
column_2 = value_2,
column_3 = value_3,
....
WHERE NOT condition;

3. NOT operator in sql delete query

DELETE column_1, column_2, column_3, ....
FROM table_name
WHERE NOT condition;

Let's look some NOT operator examples in sql.

I have mysql table named employee.

id name city country
1 justin New york US
2 robin Binghamton US
3 jessie Nottingham England
4 chris Manchester England

I want to find all employees who do not belong to US.

select * from employee where not country = 'US';

id name city country
3 jessie Nottingham England
4 chris Manchester England

If i want to find all employees id who do not belong to England country or Binghamton city.

select id from employee where not country = 'England' and not city = 'Binghamton';

id
1

Remember that first select database then run any sql query related to that database otherwise you will get 'no database selected' error.

So we can easily understand NOT operator in sql by above examples. Hope you like this tutorial.

Other important links

Sql OR Operator

Sql AND Operator

Sql LIKE Operator

Sql where clause

NOT operator in sql searches:

Tags: #mysql