SQL AND Operator
What is the AND Operator?
The AND operator in the WHERE clause is used to filter records based on multiple conditions. All conditions must be TRUE for a record to be included in the result set.
Example: Filter Records with AND
Select all customers from Spain whose names start with the letter “G”:
SELECT *
FROM Customers
WHERE Country = 'Spain'
AND CustomerName LIKE 'G%';
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
All Conditions Must Be True
The following example selects all customers where:
- Country is “Germany”
- City is “Berlin”
- PostalCode is greater than 12000
SELECT *
FROM Customers
WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > 12000;
Combining AND and OR
You can combine AND and OR operators to create more complex filters. Use parentheses to ensure the correct precedence.
Example: Select all customers from Spain whose names start with “G” or “R”:
SELECT *
FROM Customers
WHERE Country = 'Spain'
AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');
Without parentheses:
SELECT *
FROM Customers
WHERE Country = 'Spain'
AND CustomerName LIKE 'G%'
OR CustomerName LIKE 'R%';
Without parentheses, the condition would return all customers from Spain whose names start with “G”, and all customers (regardless of country) whose names start with “R”.
