MySQL AND OR and NOT Operators

MySQL AND, OR, and NOT Operators
In MySQL, logical operators AND, OR, and NOT are used in the WHERE clause to combine, broaden, or negate conditions.
They are fundamental for filtering data and are very common in exams and interviews.
What are Logical Operators?
Logical operators:
Combine multiple conditions
Return rows based on TRUE / FALSE logic
Commonly used with
WHERE,HAVING,JOIN
AND Operator
Meaning
Returns rows only if all conditions are TRUE.
Syntax
OR Operator
Meaning
Returns rows if any one condition is TRUE.
Syntax
NOT Operator
Meaning
Reverses the result of a condition.
Syntax
Combining AND, OR, NOT (Very Important)
Operator Precedence
MySQL evaluates logical operators in this order:
NOTANDOR
Risky:
Safe:
AND / OR with BETWEEN, IN, LIKE
AND / OR with NOT IN
AND_OR with NULL Handling
NULL needs special care:
AND vs OR vs NOT – Comparison
| Operator | Meaning | Result |
|---|---|---|
| AND | All conditions true | Narrow result |
| OR | Any condition true | Broad result |
| NOT | Reverse condition | Exclusion |
Common Mistakes
- Forgetting parentheses
- Confusing AND / OR logic
- Using
= NULLinstead ofIS NULL Writing complex logic without clarity
Interview Questions & MCQs (Very Important)
Q1. AND operator returns rows when:
A) Any condition is true
B) All conditions are true
C) No condition is true
D) Only first condition is true
Answer: B
Q2. OR operator returns rows when:
A) All conditions are true
B) Any condition is true
C) Condition is false
D) No condition is true
Answer: B
Q3. What does NOT operator do?
A) Joins conditions
B) Filters rows
C) Reverses condition
D) Sorts data
Answer: C
Q4. Which operator has highest precedence?
A) AND
B) OR
C) NOT
Answer: C
Q5. Which is correct?
A) Correct
B) Incorrect
Answer: A
Q6. Best practice for complex conditions?
A) Avoid AND/OR
B) Use parentheses
C) Use only OR
D) Use only AND
Answer: B
Real-Life Use Cases
- Filter employees by multiple criteria
- Search features (name OR email)
- Excluding unwanted records
- Complex report conditions
Summary
AND→ all conditions must be trueOR→ any condition can be trueNOT→ reverses conditionUse parentheses for clarity
Operator precedence matters
Very important for SQL exams & interviews
