LearnContact
Lesson 3517 min read

Logical Operators

Learn how to combine or negate multiple conditions in a query using the logical operators AND, OR, and NOT.

Introduction

A WHERE clause is rarely limited to a single condition in real applications. You often need to check several things at once — a student must be in grade "A" and older than 15, or a student must be in grade "A" or grade "B". Logical operators are what let you combine conditions like this.

MySQL provides three logical operators: AND, OR, and NOT. This lesson covers exactly how each one works, and how to keep combined conditions unambiguous using parentheses.

What You Will Learn
  • How AND requires every condition to be true.
  • How OR requires at least one condition to be true.
  • How NOT negates a condition.
  • How operator precedence works, and why parentheses matter.

The AND Operator

AND combines two or more conditions, and the overall result is true only if every single condition is true. If even one condition is false, the whole row is excluded.

Consider this students table for the examples in this lesson.

idnamegradeage
1AlexA16
2SamB17
3PriyaA15
4JonC17
5MayaA17
SELECT name, grade, age
FROM students
WHERE grade = 'A' AND age > 15;
Result
+------+-------+-----+
| name | grade | age |
+------+-------+-----+
| Alex | A     | 16  |
| Maya | A     | 17  |
+------+-------+-----+

Priya is grade "A" but is not older than 15, so she is excluded. Only rows satisfying both conditions at once are returned.

The OR Operator

OR also combines two or more conditions, but the overall result is true if at least one of the conditions is true. A row only needs to satisfy one side of an OR to be included.

SELECT name, grade, age
FROM students
WHERE grade = 'A' OR grade = 'B';
Result
+-------+-------+-----+
| name  | grade | age |
+-------+-------+-----+
| Alex  | A     | 16  |
| Sam   | B     | 17  |
| Priya | A     | 15  |
| Maya  | A     | 17  |
+-------+-------+-----+

Every student in grade "A" or grade "B" is returned; only Jon (grade "C") is excluded, since he satisfies neither condition.

The NOT Operator

NOT reverses, or negates, the condition that follows it. A row that would normally match now gets excluded, and vice versa.

SELECT name, grade, age
FROM students
WHERE NOT grade = 'A';
Result
+------+-------+-----+
| name | grade | age |
+------+-------+-----+
| Sam  | B     | 17  |
| Jon  | C     | 17  |
+------+-------+-----+
Note

WHERE NOT grade = 'A' behaves the same as WHERE grade != 'A'. NOT is most useful when negating more complex conditions, such as NOT (grade = 'A' AND age > 15).

Precedence and Parentheses

When AND and OR appear together in the same WHERE clause, MySQL evaluates AND before OR, by default — similar to how multiplication is evaluated before addition in regular math. This can produce surprising results if you are not careful.

ambiguous_without_parens.sql
-- Without parentheses, AND binds tighter than OR
SELECT name, grade, age
FROM students
WHERE grade = 'B' OR grade = 'A' AND age > 16;
Result
+------+-------+-----+
| name | grade | age |
+------+-------+-----+
| Sam  | B     | 17  |
| Maya | A     | 17  |
+------+-------+-----+

This is evaluated as grade = 'B' OR (grade = 'A' AND age > 16), which is probably not what was intended if the goal was "grade B or A, but only if older than 16." Wrapping the intended grouping in explicit parentheses removes all ambiguity.

clear_with_parens.sql
SELECT name, grade, age
FROM students
WHERE (grade = 'B' OR grade = 'A') AND age > 16;
Result
+------+-------+-----+
| name | grade | age |
+------+-------+-----+
| Sam  | B     | 17  |
| Maya | A     | 17  |
+------+-------+-----+
Always Use Parentheses When Mixing AND and OR

Even when the default precedence happens to give the result you want, add parentheses anyway. It removes any doubt about what the condition means, both for you later and for anyone else reading the query.

Worked Example: Filtering by Grade and Age

Suppose you want every student who is in grade "A" and at least 16, or any student in grade "C" regardless of age. Parentheses make each group of conditions explicit.

worked_example.sql
SELECT name, grade, age
FROM students
WHERE (grade = 'A' AND age >= 16) OR grade = 'C';
Result
+------+-------+-----+
| name | grade | age |
+------+-------+-----+
| Alex | A     | 16  |
| Jon  | C     | 17  |
| Maya | A     | 17  |
+------+-------+-----+

Priya (grade "A", age 15) is excluded because she fails the age condition and is not grade "C". Jon is included purely because he is grade "C", regardless of his age.

Common Mistakes

Avoid These Mistakes
  • Mixing AND and OR without parentheses and assuming left-to-right evaluation.
  • Writing WHERE grade = 'A' OR 'B' — this is invalid; each condition needs its own comparison, like grade = 'A' OR grade = 'B'.
  • Forgetting that NOT applies only to the single condition immediately after it, unless parentheses group more of the expression.
  • Using multiple OR conditions on the same column when the IN operator, covered later, would be clearer.

Best Practices

  • Always add parentheses when combining AND and OR in the same condition.
  • Keep related conditions grouped together visually, matching how they are grouped logically.
  • Test complex conditions on a small dataset first to confirm they behave as expected.
  • Prefer clarity over cleverness — a longer, explicit condition is better than a short, ambiguous one.

Frequently Asked Questions

Can I combine more than two conditions?

Yes, you can chain as many AND and OR conditions as needed, though grouping them clearly with parentheses becomes more important as they grow.

Does NOT work with AND and OR combined?

Yes. For example, NOT (grade = 'A' AND age > 15) negates the entire parenthesized condition, not just one part of it.

Is there a shorthand for many OR conditions on the same column?

Yes, the IN operator, covered in an upcoming lesson, replaces long chains like grade = 'A' OR grade = 'B' OR grade = 'C' with a single, cleaner expression.

Do logical operators work in HAVING as well as WHERE?

Yes, AND, OR, and NOT work the same way in HAVING, letting you combine multiple conditions on aggregated group results.

Key Takeaways

  • AND requires every combined condition to be true for a row to be included.
  • OR requires at least one combined condition to be true for a row to be included.
  • NOT negates whatever condition immediately follows it.
  • AND is evaluated before OR by default; use parentheses to make combined conditions unambiguous.

Summary

Logical operators let you build precise, multi-part filtering conditions. AND narrows results down, OR widens them, and NOT flips a condition entirely — and parentheses keep it all readable and correct.

In this lesson, you learned how AND, OR, and NOT work individually and together, and how parentheses remove ambiguity when combining them. Next, you will take a closer, dedicated look at comparison operators.

Lesson 35 Completed
  • You understand how AND, OR, and NOT each behave.
  • You know that AND is evaluated before OR by default.
  • You can use parentheses to make combined conditions unambiguous.
  • You are ready to study comparison operators in depth.
Next Lesson →

Comparison Operators