The WHERE clause can be combined with AND, OR, and NOT operators to filter records based on more than one condition.
AND operator displays a record if all the conditions separated by AND are TRUE.OR operator displays a record if any of the conditions separated by OR is TRUE.NOT operator displays a record if the condition(s) is NOT TRUE.SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Below is a selection from the Customers table:
Customers Table:
| CustomerID | CustomerName | City | Country |
|---|---|---|---|
| 1 | Alfreds Futterkiste | Berlin | Germany |
| 2 | Ana Trujillo Emparedados | México D.F. | Mexico |
| 3 | Antonio Moreno Taquería | México D.F. | Mexico |
| 4 | Berglunds snabbköp | Luleå | Sweden |
ANDThe following SQL statement selects all customers from the country "Germany" AND has a CustomerID of 1.
SELECT * FROM Customers WHERE Country = 'Germany' AND CustomerID = 1;
ORThe following SQL statement selects all customers from the country "Germany" OR the country "Sweden".
SELECT * FROM Customers WHERE Country = 'Germany' OR Country = 'Sweden';
NOTThe following SQL statement selects all customers that are NOT from the country "Germany".
SELECT * FROM Customers WHERE NOT Country = 'Germany';
AND, OR, and NOTYou can also combine these operators. Use parentheses to form complex expressions and control the order of evaluation.
The following SQL statement selects all customers from "Mexico" that have a CustomerID of 2 OR 3.
SELECT * FROM Customers WHERE Country = 'Mexico' AND (CustomerID = 2 OR CustomerID = 3);