← Back to MySQL Course | Chapter 6: Filtering & Sorting | Lesson 2 of 8

AND OR NOT Operators

The AND Operator

AND combines multiple conditions such that every single one must be true for a row to appear in the results, letting you narrow a search down with increasing precision as you add more clauses.

Example: The AND Operator

sql
SELECT * FROM users WHERE age > 18 AND city = 'Patna';

The OR Operator

OR requires only one of the combined conditions to be true, which broadens a search rather than narrowing it — useful when you want rows matching any of several acceptable criteria.

Example: The OR Operator

sql
SELECT * FROM users WHERE city = 'Patna' OR city = 'Delhi';

The NOT Operator

NOT inverts whatever condition follows it, returning rows where that condition is false — handy for expressing exclusions like 'everyone except inactive users' more naturally than a negative comparison.

Example: The NOT Operator

sql
SELECT * FROM users WHERE NOT status = 'inactive';

Combining AND with OR

When AND and OR appear together in the same WHERE clause, wrapping the OR conditions in parentheses is essential, since MySQL evaluates AND with higher precedence and can group conditions unexpectedly otherwise.

Example: Combining AND with OR

sql
SELECT * FROM users WHERE city = 'Patna' AND (age > 18 OR status = 'verified');

Combining NOT with other filters

NOT can also wrap an entire parenthesized group of conditions at once, letting you exclude a whole compound match rather than negating just a single simple comparison.

Example: Combining NOT with other filters

sql
SELECT * FROM users WHERE NOT (city = 'Patna' AND age > 18);
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.