[Basic Grammar] SQL AND Operator
The WHERE Clause can contain one or many AND Operators. It is usd to filter records based on more than one condition. AND and OR Operators are like values(conditions) that the questions asks for.
Example
Select all customers from Spain that starts 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
Example
SELECT * FROM Customers
WHERE Country = 'South Korea'
AND City = 'Seoul'
AND PostalCode < 12000;
- Combining AND and OR
We can combine AND and OR operators in WHERE Clause.
Example
Select all Korean customers that starts with either "H" or "R":
SELECT * FROM Customers
WHERE Country = 'South Korea' AND CustomerName LIKE 'H%' OR CustomerName LIKE 'R%');
Without parenthesis, the select statement will return all the customers that start with G and R, regardless of the country value:
Example
Select all customers that either: are from Spain and starts with either "G" or "R":
SELECT * FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';
Exercises
1. Retrieve all orders where the "ProductID" is 101 and the "Quantity" is 5.
SELECT* FROM Orders
WHERE ProductID = 101 AND Qunatity = 5;
2. Fetch all customers with the "Country" column set to 'France' and the "Region" column set to 'Provence-Alpes-Côte d'Azur.'
SELECT * FROM Customers
WHERE Country = 'France' AND Region = 'Provence-Alpes-Côte d'Azur';
3. Retrieve all products from the "Products" table that have a price less than $50 and are in the "Electronics" category.
SELECT * FROM Products
WHERE Price < 50 AND Category = 'Electronics';