SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The NOT
operator in SQL is used to negate a condition, essentially reversing its result. It's employed in various clauses and scenarios to filter the data contrary to a given condition.
NOT in WHERE clause:
You can use the NOT
operator in the WHERE
clause to exclude rows that match a particular condition.
SELECT * FROM employees WHERE NOT department = 'IT';
This query selects all employees who do not work in the 'IT' department.
NOT LIKE:
This combination is used to exclude rows that match a particular pattern.
SELECT * FROM employees WHERE name NOT LIKE 'A%';
This retrieves all employees whose names don't start with the letter 'A'.
NOT IN:
Used to filter out rows that match any value in a list.
SELECT * FROM employees WHERE department NOT IN ('IT', 'HR');
This fetches employees who aren't in the 'IT' or 'HR' departments.
NOT EXISTS:
Used in conjunction with a subquery to filter out rows that would result in rows from the subquery.
SELECT * FROM employees e WHERE NOT EXISTS ( SELECT 1 FROM department_leads d WHERE d.employee_id = e.id );
This example would fetch all employees who aren't department leads.
NOT BETWEEN:
Retrieves rows outside of a specified range.
SELECT * FROM products WHERE price NOT BETWEEN 10 AND 50;
This gets products that are either priced below $10 or above $50.
Combining NOT with AND & OR:
NOT
can be combined with AND
and OR
logical operators.
SELECT * FROM employees WHERE NOT (department = 'IT' OR salary > 60000);
This query fetches employees who are neither in the 'IT' department nor have salaries greater than 60,000.
While using the NOT
operator, it's essential to ensure clarity in the conditions, especially when multiple conditions are combined. It might be beneficial in complex scenarios to use parentheses ()
to group conditions clearly and ensure the intended logical evaluation order.
How to use NOT in SQL:
SELECT * FROM employees WHERE NOT department_id = 2;
Negating conditions with NOT in WHERE clause:
SELECT * FROM orders WHERE NOT order_status = 'Shipped';
Using NOT with AND and OR operators in SQL:
SELECT * FROM products WHERE category = 'Electronics' AND NOT (price > 1000 OR stock_quantity < 10);
Dealing with NULL values using NOT in SQL:
SELECT * FROM customers WHERE NOT email IS NULL;
NOT LIKE and NOT IN operators in SQL:
SELECT * FROM products WHERE NOT product_name LIKE 'Old%' AND NOT category_id IN (1, 3, 5);
NOT EXISTS and NOT BETWEEN in SQL:
SELECT * FROM employees e WHERE NOT EXISTS ( SELECT 1 FROM salaries s WHERE e.employee_id = s.employee_id AND s.salary > 50000 ); SELECT * FROM orders WHERE NOT order_date BETWEEN '2022-01-01' AND '2022-12-31';
Combining NOT with other comparison operators:
SELECT * FROM products WHERE NOT unit_price > 100;
Using NOT with subqueries in SQL:
SELECT * FROM employees WHERE NOT EXISTS ( SELECT 1 FROM terminated_employees WHERE employees.employee_id = terminated_employees.employee_id );
Alternatives to NOT for condition negation in SQL:
-- Using negative conditions SELECT * FROM products WHERE category <> 'Electronics'; -- Restructuring query logic SELECT * FROM orders WHERE order_status = 'Pending' OR order_status = 'Processing';