The SQL AND Operator
The WHERE clause can contain one or many AND operators.
The AND operator is used to filter records based on more than one condition.
Note: The AND operator displays a record if all the conditions are TRUE.
AND Syntax
Demo Database
Below is a selection from the Customers table:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
AND Example
Select all customers from Spain starting with 'G':
All Conditions Must Be True
Select customers where Country is Brazil, City is Rio de Janeiro, and CustomerID > 50:
AND vs. OR
| Operator | Description | Result |
|---|---|---|
| AND | Returns TRUE if all conditions are TRUE | All must match |
| OR | Returns TRUE if any condition is TRUE | At least one must match |
Combining AND and OR
Select Spanish customers starting with G or R (use parentheses!):
Without parentheses, results will be different:
- Always use parentheses when combining AND and OR
- AND has higher precedence than OR
- Multiple AND operators can be used
- All AND conditions must be true
Important: Use parentheses to make your intention clear and avoid unexpected results.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP
Sign in to track your progress and earn XP!
Exercise 1 of 2Easy
Which of the following is a best practice in programming?
10 XP~2 min
Exercise 2 of 2Easy
Code that is easy to read and understand is called ___ code.
10 XP~2 min