The MySQL AND, OR and NOT Operators
The WHERE
clause can be combined with AND
, OR
, and NOT
operators. The AND
and OR
operators are used to filter records based on more than one condition:
- The
AND
operator displays a record if all the conditions separated byAND
are TRUE. - The
OR
operator displays a record if any of the conditions separated byOR
is TRUE.
The NOT
operator displays a record if the condition(s) is NOT TRUE.
AND Example
SELECT * FROM Customers
WHERE Country = 'Bhadrapur' AND City = 'Birtamode';
WHERE Country = 'Bhadrapur' AND City = 'Birtamode';
OR Example
SELECT * FROM Customers
WHERE City = 'Pokhara' OR City = 'Kathmandu';
WHERE City = 'Pokhara' OR City = 'Kathmandu';
NOT Example
SELECT * FROM Customers
WHERE NOT Country = 'Nepal';
WHERE NOT Country = 'Nepal';
Combining AND, OR and NOT
SELECT * FROM Customers
WHERE Country = 'Nepal' AND (City = 'Bhadrapur' OR City = 'Damak');
WHERE Country = 'Nepal' AND (City = 'Bhadrapur' OR City = 'Damak');