SQL NOT Operator

The NOT is a logical operator in Structured Query Language.

This Operator in SQL negates the output of any boolean expression.

This operator compares the given value to each value and returns those values which do not satisfy the condition.

NOT Operator operator is mainly used in WHERE clause with the INSERT, UPDATE, DELETE and UPDATE SQL statements.

Syntax of NOT Operator in Structured Query Language:

In the SQL syntax, we have to specify the NOT keyword in the WHERE clause with the condition.

Example of NOT Operator in SQL

To understand the query of NOT Operator in Structured query Language, we have to create the new table called Worker_Info. This table contains the details of workers working in the industry.

The following query creates the Worker_Info table in the Industry Database:

The following INSERT queries insert the records of multiple workers who work in the Industry:

The following query shows the data of the Worker_Info table.


Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
5001ArushSharma10012020-01-02Delhi20000
5002BulbulRoy10022019-12-31Delhi38000
5004SaurabhRoy10012020-10-10Mumbai45000
5005ShivaniSinghania10012019-07-15Kolkata42000
5006AvinashSharma10022019-11-11Delhi28000
5007ShyamBesas10032021-06-21Lucknow35000

The following query shows the records of those workers from the above table who does not belong to Kolkata City:


Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
5001ArushSharma10012020-01-02Delhi20000
5002BulbulRoy10022019-12-31Delhi38000
5004SaurabhRoy10012020-10-10Mumbai45000
5006AvinashSharma10022019-11-11Delhi28000
5007ShyamBesas10032021-06-21Lucknow35000

The following query does not show the records of those workers from the above table whose Salary is greater than 30000.

Output:

Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
5001ArushSharma10012020-01-02Delhi20000
5006AvinashSharma10022019-11-11Delhi28000

NOT Operator with IN operator in SQL

We can also use the NOT operator with IN operator in SQL for filtering those records which do not pass in the parenthesis of IN operator.

Syntax of NOT Operator with IN operator

Example of NOT Operator with IN operator

The following query shows those records of workers whose Worker_ID is not passed in the IN operator:

Output:

Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
5001ArushSharma10012020-01-02Delhi20000
5002BulbulRoy10022019-12-31Delhi38000
5006AvinashSharma10022019-11-11Delhi28000

NOT Operator with the LIKE operator in SQL

We can also use the NOT operator with the LIKE operator in SQL for filtering those values which do not match with the specified pattern.

Syntax of NOT Operator with LIKE operator

Examples of NOT Operator with LIKE operator

Example 1: The following query shows those records of workers from the worker_Info table whose First Name does not start with A.

Output:

Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
5002BulbulRoy10022019-12-31Delhi38000
5004SaurabhRoy10012020-10-10Mumbai45000
5005ShivaniSinghania10012019-07-15Kolkata42000
5007ShyamBesas10032021-06-21Lucknow35000

Example 2: The following query does not show those records of workers from the worker_Info table whose City name begins with D character and ends with i character.

Output:

Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_Joining_DateWorker_CityWorker_Salary
5004SaurabhRoy10012020-10-10Mumbai45000
5005ShivaniSinghania10012019-07-15Kolkata42000
5007ShyamBesas10032021-06-21Lucknow35000





Latest Courses