HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL AND, OR, NOT Operators

A WHERE clause with a single condition is useful, but real-world queries often need to check multiple conditions at once. MySQL provides three logical operators — AND, OR, and NOT — to combine conditions and build more precise filters.


Sample Table

idnamecityage
1Rahim UddinDhaka22
2Sara BegumChittagong20
3Karim AliSylhet23
4Nila AkterDhaka21
5Rafi HossainRajshahi24

AND Operator

AND returns rows where all conditions are true. If even one condition fails, the row is excluded.

-- Students from Dhaka who are older than 20
SELECT * FROM students
WHERE city = 'Dhaka' AND age > 20;
idnamecityage
1Rahim UddinDhaka22
4Nila AkterDhaka21

Both conditions must be satisfied: the student must be from Dhaka and must be older than 20.


OR Operator

OR returns rows where at least one condition is true. A row only needs to pass one of the conditions to be included.

-- Students from either Dhaka or Sylhet
SELECT * FROM students
WHERE city = 'Dhaka' OR city = 'Sylhet';
idnamecityage
1Rahim UddinDhaka22
3Karim AliSylhet23
4Nila AkterDhaka21

NOT Operator

NOT reverses a condition — it returns rows where the condition is false.

-- All students who are NOT from Dhaka
SELECT * FROM students
WHERE NOT city = 'Dhaka';
idnamecityage
2Sara BegumChittagong20
3Karim AliSylhet23
5Rafi HossainRajshahi24
Note: NOT city = 'Dhaka' is the same as city != 'Dhaka'. NOT becomes more valuable when combined with other operators like IN, BETWEEN, and LIKE.

Combining AND and OR

You can mix AND and OR in the same condition, but be careful — AND has higher precedence than OR. This means MySQL evaluates AND conditions first, before OR.

This query may not behave as expected without parentheses:

-- Ambiguous: AND is evaluated first
SELECT * FROM students
WHERE city = 'Dhaka' OR city = 'Sylhet' AND age > 22;

MySQL reads this as: city = 'Dhaka' OR (city = 'Sylhet' AND age > 22) — returning all Dhaka students regardless of age, plus only Sylhet students older than 22.

Use parentheses to make the logic explicit and avoid surprises:

-- Clear intent: students from Dhaka or Sylhet, who are also older than 22
SELECT * FROM students
WHERE (city = 'Dhaka' OR city = 'Sylhet') AND age > 22;
Best Practice: Whenever you mix AND and OR, always use parentheses to group your conditions. It makes the logic clear to both MySQL and anyone reading the query.

Multiple ANDs and ORs

You can chain as many logical operators as needed:

-- Students aged between 21 and 23, from Dhaka or Chittagong
SELECT * FROM students
WHERE (age >= 21 AND age <= 23)
  AND (city = 'Dhaka' OR city = 'Chittagong');

Key Points to Remember

  • AND — all conditions must be true for the row to be included.
  • OR — at least one condition must be true.
  • NOT — reverses a condition; useful with IN, BETWEEN, and LIKE.
  • AND has higher precedence than OR — MySQL evaluates AND first.
  • Always use parentheses when mixing AND and OR to make your intent clear and avoid unexpected results.
What's next? The next lesson covers ORDER BY — how to sort your query results in ascending or descending order by one or more columns.