bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL AND Operator

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind SQL AND Operator?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ Customers
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Combining AND and OR
All Conditions Must Be True
The SQL AND Operator

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.

The following SQL selects all customers from Spain that starts with the letter 'G':

Example

SELECT *

FROM Customers

WHERE Country = 'Spain' AND CustomerName LIKE 'G%';

AND Syntax

SELECT column1 , column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ... ;

Demo Database

Below is a selection from the Customers table used in the examples:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna 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

All Conditions Must Be True

The following SQL selects all customers where Country is "Brazil" AND City is "Rio de Janeiro" AND CustomerID is higher than 50:

Example

  SELECT * FROM Customers
WHERE Country = 'Brazil'
AND City = 'Rio de
  Janeiro'
AND CustomerID > 50;

AND vs. OR

The AND operator displays a record if all the conditions are TRUE.

The OR operator displays a record if any of the conditions are TRUE.

Combining AND and OR

You can also combine AND and OR operators.

The following SQL selects all customers from Spain that starts with a "G" or an "R" (make sure to use parenthesis to get the correct result):

Example

SELECT * FROM Customers

WHERE Country = 'Spain'
AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');

Without parenthesis, the SQL above will return all customers from Spain that starts with a "G", plus all customers that starts with an "R", regardless of the country value:

Example

SELECT * FROM Customers

WHERE Country = 'Spain'
AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';

Previous

SQL ORDER BY Keyword

Next

SQL OR Operator