bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

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

This chapter

Overview
46

Lessons

115m

Read time

1. SQL Tutorial2. Introduction to SQL3. SQL Syntax4. SQL SELECT Statement5. SQL SELECT DISTINCT Statement6. SQL WHERE Clause7. SQL ORDER BY Keyword8. SQL AND Operator9. SQL OR Operator10. SQL NOT Operator11. SQL INSERT INTO Statement12. SQL NULL Values13. SQL UPDATE Statement14. SQL DELETE Statement15. SQL SELECT TOP, LIMIT and FETCH FIRST16. SQL Aggregate Functions17. SQL MIN() F unction18. SQL MAX() Function19. SQL COUNT() Function20. SQL SUM() Function21. SQL AVG() Function22. SQL LIKE Operator23. SQL Wildcards24. SQL IN Operator25. SQL BETWEEN Operator26. SQL Aliases27. SQL Joins28. SQL INNER JOIN29. SQL LEFT JOIN30. SQL RIGHT JOIN31. SQL FULL JOIN32. SQL Self Join33. SQL UNION Operator34. SQL UNION ALL Operator35. SQL GROUP BY Statement36. SQL HAVING Clause37. SQL EXISTS Operator38. SQL ANY Operator39. SQL ALL Operator40. SQL SELECT INTO Statement41. SQL INSERT INTO SELECT Statement42. SQL CASE Expression43. SQL NULL Functions44. SQL Stored Procedures45. SQL Comments46. SQL Operators

On this page

The SQL AND OperatorAND SyntaxDemo DatabaseAll Conditions Must Be TrueAND vs. ORCombining AND and OR