The NOT Operator
You'll learn how to use NOT to reverse your filter logic and exclude specific rows from query results.
What NOT Does
The NOT operator inverts any condition in SQL, turning TRUE into FALSE and FALSE into TRUE. It's like saying "show me everything except what matches this condition." While AND and OR combine multiple conditions, NOT flips the logic of a single condition to exclude rather than include.
How to Use NOT
You place NOT directly before the condition you want to reverse:
SELECT *
FROM customers
WHERE NOT country = 'USA';
This retrieves all customers who are not from the USA—the opposite of WHERE country = 'USA'.
Common NOT Patterns
Excluding multiple values:
SELECT product_name
FROM products
WHERE category NOT IN ('Electronics', 'Toys');
Reversing range logic:
SELECT *
FROM orders
WHERE NOT (price BETWEEN 50 AND 100);
This finds orders priced under 50 or over 100.
Excluding pattern matches:
SELECT email
FROM users
WHERE email NOT LIKE '%@gmail.com';
Think of It This Way
Imagine filtering a deck of cards. WHERE suit = 'Hearts' shows you hearts. WHERE NOT suit = 'Hearts' shows you everything but hearts—clubs, diamonds, and spades. You're defining your results by what they're not, rather than what they are.
Key Takeaway: The NOT operator inverts filter conditions, allowing you to exclude specific rows by reversing the logic of =, IN, BETWEEN, LIKE, and other comparison operators—essential when it's easier to define what you don't want than what you do.