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:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo EmparedadosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

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

OperatorDescriptionResult
ANDReturns TRUE if all conditions are TRUEAll must match
ORReturns TRUE if any condition is TRUEAt 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