DevLearningTools

LEARN · SQL

SQL NOT

NOT reverses a condition's result. Learn the syntax, and De Morgan's law, how NOT distributes across a parenthesized AND or OR.

AND and OR combine conditions. NOT does something different: it reverses a single condition's result, true becomes false and false becomes true.

Learning Objectives

  • Reverse a condition's result with NOT.
  • Apply NOT to a parenthesized group of conditions.
  • Understand De Morgan's law, how NOT distributes across AND and OR.

Condition

department = 'Engineering'

NOT

Reverses true/false

Matching Rows

Everywhere the condition was false

NOT: Reversing a Condition

NOT flips a condition's result. NOT department = 'Engineering' matches every row where that equality is false, functionally the same as department <> 'Engineering', but NOT can prefix any condition, including more complex ones.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
SELECT first_name, department
FROM employees
WHERE NOT department = 'Engineering';

NOT on a Parenthesized Group

NOT can prefix a parenthesized group of conditions, reversing the whole group's combined result, not just one condition.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
SELECT first_name, department, salary
FROM employees
WHERE NOT (department = 'Engineering' AND salary > 95000);

De Morgan's Law

Distributing NOT across a parenthesized AND or OR flips the operator too, AND becomes OR, and OR becomes AND. This is a rule from formal logic called De Morgan's law, and it's a genuinely common point of confusion worth internalizing.

NOT (A AND B)
  • Equivalent to (NOT A) OR (NOT B)
NOT (A OR B)
  • Equivalent to (NOT A) AND (NOT B)

Try It Yourself

Rewrite WHERE NOT (department = 'Engineering' AND salary > 95000) without NOT, using De Morgan's law.

Answer
SELECT first_name, department, salary
FROM employees
WHERE department <> 'Engineering' OR salary <= 95000;

Frequently Asked Questions

Is NOT column = value the same as column <> value?

Yes, functionally identical for a simple equality check. NOT becomes more useful once it's applied to something more complex than a single equality, like a parenthesized group, LIKE, or BETWEEN.

Can NOT be combined with LIKE, IN, and BETWEEN?

Yes, NOT LIKE, NOT IN, and NOT BETWEEN are all valid and commonly used, each covered in their own lesson later in this module.

Common Beginner Mistakes

Forgetting to flip the operator when distributing NOT across parentheses

NOT (A AND B) becomes (NOT A) OR (NOT B), the AND flips to OR. Simplifying it to (NOT A) AND (NOT B) instead, keeping AND, is a common logic error.

Using NOT column = value instead of the clearer column <> value

Both work identically for a simple equality, but <> (or !=) is more direct and readable for that specific case. Save NOT for conditions where it's genuinely needed, like negating a parenthesized group.

Best Practices

  • Use <> or != instead of NOT column = value for a simple equality check, it reads more directly.
  • Reserve NOT for negating something more complex than a single equality, a parenthesized group, LIKE, IN, or BETWEEN.
  • Double-check De Morgan's law by hand when distributing NOT across AND/OR, it's easy to forget the operator flip.

Interview Questions

Apply De Morgan's law to NOT (status = 'active' OR status = 'pending').

status <> 'active' AND status <> 'pending' — the OR flips to AND, and NOT is distributed onto each individual condition.

Why is NOT (A AND B) not the same as (NOT A) AND (NOT B)?

NOT (A AND B) is true whenever at least one of A or B is false. (NOT A) AND (NOT B) is only true when both A and B are false. They agree when both A and B are false, but disagree whenever exactly one of them is true, De Morgan's law gives the actually-correct equivalence: NOT (A AND B) = (NOT A) OR (NOT B).

Summary

This lesson covered reversing a condition with NOT, applying it to a parenthesized group, and De Morgan's law, how NOT distributes across AND (flipping to OR) and OR (flipping to AND).

What's Next?

The next lesson covers LIKE, pattern matching inside text values.

</>

Ready to Practice SQL?

Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.