Advanced NOT & Negation Logic: Functions
Module: Advanced Filtering
-- Basic NOT
SELECT * FROM customers WHERE NOT status = 'cancelled';
-- NOT IN (dangerous with NULL)
SELECT * FROM products WHERE category_id NOT IN (1, 2, 3);
-- NOT EXISTS (NULL-safe)
SELECT * FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
-- NOT LIKE
SELECT * FROM users WHERE email NOT LIKE '%@test.com';
-- Combining NOT with AND/OR
SELECT * FROM orders
WHERE NOT (status = 'cancelled' OR status = 'refunded')
AND total > 100;
-- NULL-safe negation
SELECT * FROM products
WHERE category_id NOT IN (SELECT id FROM categories WHERE id IS NOT NULL);
NOT negates any boolean condition
NOT IN excludes values in list
NOT EXISTS finds rows without related data
NOT LIKE excludes pattern matches
NOT with NULL returns NULL (not true)
NOT IN with NULL in list returns no rows
Core references in this topic include WHERE, =, != / <>. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.
=
Returns rows where the left and right values are exactly equal.
column = value
Use with exact matches. Do not use = NULL.
!= / <>
Returns rows where the compared values are not equal.
column <> value
SQL supports both <> and != in many engines, but <> is the portable form.
<, >, <=, >=
Range comparison operators for less-than, greater-than, and inclusive boundary checks.
salary >= 80000
IS NULL / IS NOT NULL
Tests whether a value is missing. SQL NULL semantics require dedicated NULL predicates.