Javatpoint Logo
Javatpoint Logo

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5005 Shivani Singhania 1001 2019-07-15 Kolkata 42000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

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


Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

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

Output:

Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5005 Shivani Singhania 1001 2019-07-15 Kolkata 42000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5005 Shivani Singhania 1001 2019-07-15 Kolkata 42000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA