SQL Practice Logo

SQLPractice Online

Advanced NOT & Negation Logic: Concept

Module: Advanced Filtering

Negation logic (NOT, NOT IN, NOT EXISTS, NOT LIKE) filters by exclusion. NULL handling is critical - negation with NULL can produce unexpected results.

**NOT Operator:**

Negates any condition.

WHERE NOT status = 'cancelled'

Equivalent to: WHERE status != 'cancelled' OR status <> 'cancelled'

**NOT IN:**

Excludes values in list.

WHERE id NOT IN (1, 2, 3)

DANGER: NOT IN with NULL returns no rows!

**NOT EXISTS:**

Finds rows without related data.

WHERE NOT EXISTS (SELECT 1 FROM orders WHERE customer_id = customers.id)

NULL-safe, preferred over NOT IN.

**NOT LIKE:**

Excludes pattern matches.

WHERE name NOT LIKE 'Test%'

**NULL Behavior:**

- NOT NULL = NULL (not true, not false)

- NOT IN with NULL in list returns no rows

- NOT EXISTS handles NULL correctly

- Use IS NOT NULL explicitly

Critical for data quality checks, finding gaps, and exclusion logic. Common in data analysis and business intelligence.

Negation finds missing data: customers without orders, products not in any category, users who haven't logged in, records not matching patterns.