WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL AND & OR Operators


The AND & OR operators conditions may be combined to test multiple conditions in a query. These two operators are called conjunctive operators.


The SQL AND & OR Operators

The AND operator displays a record True if both expression are true.

The OR operator displays a record True if either expression are true.


Example Database

In this tutorial we will use the sample database.

Below is a selection from the "Customers" table:

CustomerID FirstName City Country
1

Eva Dallas Germany
2 Maria Houston. Mexico
3 Jhon Texas Mexico
4

Rajesh India UK
5 Stephine New york USA

AND Operator Example

The following SQL statement selects all customers from the country "USA" AND the city "New york", in the "Customers" table:

Example

SELECT * FROM Customers
WHERE Country='USA'
AND City='New york';

OR Operator Example

The following SQL statement selects all customers from the city "Delhi" OR "New york", in the "Customers" table: 

Example

SELECT * FROM Customers
WHERE City='Delhi'
OR City='New york';

Combining AND & OR

When combining these conditions, it is important to use parentheses, so that the order to evaluate each condition is known.

The following SQL statement selects all customers from the country "India" AND the city must be equal to "Delhi" OR "New york", in the "Customers" table:

Example

SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Delhi' OR City='New york');
You can nest as many conditions as you need.