SQL NOT Operator
Example Table
We will use the following table named employees for our examples:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
department VARCHAR(50)
);
INSERT INTO employees (employee_id, employee_name, department) VALUES
(1, 'Alice Johnson', 'HR'),
(2, 'Bob Smith', 'IT'),
(3, 'Charlie Brown', 'Finance'),
(4, 'Diana Prince', 'IT'),
(5, 'Eve Davis', 'Marketing');
Employees Table
| employee_id | employee_name | department |
|---|---|---|
| 1 | Alice Johnson | HR |
| 2 | Bob Smith | IT |
| 3 | Charlie Brown | Finance |
| 4 | Diana Prince | IT |
| 5 | Eve Davis | Marketing |
Using SQL NOT Operator
SQL NOT is used to negate a condition in a query.
Example: Find Employees Not in IT Department
To find employees who are not in the IT department, use the following query:
SELECT employee_name, department
FROM employees
WHERE department NOT IN ('IT');
Result:
| employee_name | Alice Johnson |
|---|---|
| department | HR |
| employee_name | Charlie Brown |
| department | Finance |
| employee_name | Eve Davis |
| department | Marketing |
Example: Find Employees Not Named 'Bob Smith'
To find employees whose name is not 'Bob Smith', use the following query:
SELECT employee_name, department
FROM employees
WHERE employee_name != 'Bob Smith';
Result:
| employee_name | Alice Johnson |
|---|---|
| department | HR |
| employee_name | Charlie Brown |
| department | Finance |
| employee_name | Diana Prince |
| department | IT |
| employee_name | Eve Davis |
| department | Marketing |