De Morgan's Law in SQL

Logic can quickly become convoluted when you start nesting NOT, AND, and OR in SQL. De Morgan’s Law provides a way to simplify.

The Law

\[\neg (A \land B) = (\neg A) \lor (\neg B)\] \[\neg (A \lor B) = (\neg A) \land (\neg B)\]

This gives us a way to rewrite negated conditions into something more readable in SQL.

Imagine you need to find users who are not from India or the USA.

A common approach:

SELECT * FROM users
WHERE NOT (country = 'India' OR country = 'USA');

Using De Morgan’s Law, you can simplify:

SELECT * FROM users
WHERE country <> 'India' AND country <> 'USA';

It is logically equivalent and simpler.