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

AND OR NOT Operators

AND, OR and NOT are like the words you use when choosing a snack: both this and that, either this or that, or anything but this.
Syntax
sql
WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition

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);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing AND and OR without parentheses, so AND is evaluated first and the result is not what you meant.
  2. Writing WHERE city = Patna OR Delhi, which is not a comparison with Delhi.
  3. Using NOT before the column instead of the condition, like WHERE NOT city = Patna misread, instead of <> or clear parentheses.
🔒

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.