In SQL, NOT operator is a negate operator which means it will return a result as just opposite for the defined conditions in SQL statements.
In SQL, we can use a NOT operator with EXISTS, BETWEEN, LIKE, IN, etc., based on our requirements. We will check this SQL not operator with an example for that create “EmployeeDetails” table by using the following script in your SQL database.
Once we execute the above script, a new table called “EmployeeDetails” will create and return the following result.
Now run the following examples to check NOT with LIKE, IN, EXISTS in the SQL server.
The following SQL not operator with like statement will return all the employees whose location does not contain a word 'en' anywhere in the location column because we used a NOT keyword with a LIKE operator and mentioned a pattern like '%en%'.
When we execute the above SQL like operator with not keyword example, we will get the following result.
Generally, if we use IN operator in SQL it will return data whose column value is within the set of values. Suppose if we use NOT keyword with IN operator, it will return the data whose column value is not in the set of values.
The following SQL statement will return all the records whose location column values are not in the mentioned values.
When we execute the above SQL IN operator with not keyword example, we will get the following result.
In SQL, if we use EXISTS operator it will return all the records whose values match with the subquery values. Suppose if we use NOT with EXISTS operator, it will return all the records whose values do not match with the subquery returned values.
The following SQL statement will return data from EmployeeDetails whose values do not exist in subquery returned values.
When we execute the above SQL not keyword with the EXISTS operator statement, we will get the result below.