The SQL NOT Operator
The SQL NOT operator is used to reverse the outcome of a condition in SQL. It’s used to filter out rows that don’t fit a certain criteria.
The SQL NOT operator negates the condition’s boolean value, making it true if the initial condition is true and false otherwise.
Here is the basic syntax of the NOT operator in SQL:
SQL> SELECT column1, column2, column3 FROM table_name
WHERE NOT condition;
Let’s have a look at an example using the same “Employees” table:
Assume we have an “Employees” table with the following data:
EmployeeID FirstName LastName Department Salary
1 John Sharma HR 50000
2 Jane Verma Sales 55000
3 Robert Johnson IT 60000
4 Lisa Pandit Marketing 52000
5 Sarah Lee HR 48000
Let’s say you wish to find employees who don’t work for the HR division. To reject the statement “Department = ‘HR’,” use the NOT operator.
SQL Not Operator with an Example
SQL> SELECT EmployeeID, FirstName, LastName, Department, Salary FROM Employees
WHERE NOT Department = ‘HR’;
The above SQL query result will be:
EmployeeID FirstName LastName Department Salary
2 Jane Verma Sales 55000
3 Robert Johnson IT 60000
4 Lisa Pandit Marketing 52000
As Department = HR is denied in this example by the NOT operator, only employees who do not work in the HR department are chosen.
You can use the NOT operator to select rows based on the converse of a given condition.