The NOT operator in SQL is used to negate a condition or expression. It essentially reverses the result of a condition. If the condition is TRUE, using NOT will make it FALSE; if the condition is FALSE, using NOT will make it TRUE.
Syntax
Example 1: Using NOT with a simple condition
Imagine you have a table Employees and you want to find all employees who are not in the "Sales" department:
This query will return all rows where the Department is not "Sales".
Example 2: Using NOT with IN to filter out multiple values
You can also use NOT with the IN operator to exclude multiple values from a list:
This query will return all employees who are not in the "Sales", "Marketing", or "HR" departments.
Example 3: Using NOT with BETWEEN
You can negate a BETWEEN condition as well:
This query will return all employees whose salary is not between 50,000 and 100,000.
Example 4: Using NOT with LIKE
You can use NOT LIKE to exclude patterns:
This will return all employees whose names do not start with the letter 'A'.
Example 5: Using NOT with EXISTS
You can also use NOT EXISTS to filter out rows based on the existence of a subquery:
This query will return employees who are not assigned to any project (assuming the Projects table contains the EmployeeID).
Summary
- The
NOToperator negates a condition. - It's commonly used with
=(equality),IN,BETWEEN,LIKE, andEXISTSto filter out values or conditions.
No comments:
Post a Comment