The MySQL NOT operator reverses the boolean result of a condition, returning rows where the condition evaluates to false. Developers combine NOT with IN, BETWEEN, LIKE, EXISTS, and IS NULL to exclude unwanted records.
Place NOT before a condition or inside compound predicates such as NOT IN and IS NOT NULL:
SELECT columns
FROM table_name
WHERE NOT condition;
This query returns active items from demo_products while excluding discontinued and sample categories.
SELECT id, product_name, category, price
FROM demo_products
WHERE category NOT IN ('discontinued', 'sample')
AND deleted_at IS NULL
ORDER BY price DESC;
| id | product_name | category | price |
|---|---|---|---|
| 14 | Standing Desk | Furniture | 299.50 |
| 8 | Wireless Headphones | Electronics | 89.99 |
NOT IN + NULL Trap: If a NOT IN (subquery) list contains even a single NULL value, the entire condition evaluates to UNKNOWN and returns zero rows. Filter out NULL inside the subquery (WHERE id IS NOT NULL) or prefer NOT EXISTS for subquery exclusion.status = 'active') narrow down B-tree ranges efficiently, whereas NOT status = 'archived' often reads most of the table. Use positive allowlists whenever your status set is small.WHERE NOT is_disabled = 0 with WHERE is_disabled = 0 so queries stay clear to future reviewers.WHERE id NOT IN (1, 2, NULL) return zero rows?In SQL three-valued logic, id != NULL evaluates to UNKNOWN, making the entire AND-chained NOT IN check UNKNOWN.
NOT EXISTS safer than NOT IN for subqueries?Yes, NOT EXISTS ignores NULL rows safely and handles correlated anti-joins cleanly in MySQL 8.
!= and <> in MySQL?Both mean "not equal to" and behave identically in MySQL; <> is the ANSI SQL standard form.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.