Not Operator in SQL
NOT Operator in SQL
The NOT operator is a logical negation operator used to reverse the result of a condition or predicate.
? Usage:
SELECT column1, column2, ...FROM table_nameWHERE NOT condition;? Examples:
Select employees who are not in department 10:
SELECT employee_name, department_idFROM employeesWHERE NOT department_id = 10;Equivalent to:
WHERE department_id <> 10;Select customers who do not have orders in 2023:
SELECT customer_nameFROM customersWHERE NOT EXISTS ( SELECT 1 FROM orders WHERE orders.customer_id = customers.customer_id AND order_date BETWEEN '2023-01-01' AND '2023-12-31');? Notes:
NOTcan be used with any condition to invert its truth value.Often combined with
IN,EXISTS,LIKE, etc.
For example:WHERE NOT IN (...),WHERE NOT LIKE 'A%', etc.
Need examples with NOT combined with other operators?