And – Or

In SQL, the AND and OR operators are used to combine multiple conditions in a WHERE clause of a SELECT statement.

The AND operator is used to combine two or more conditions, and it returns true only when all the conditions are satisfied. For example, the following query returns all the records from the Customers table where the City is ‘New York’ and the Country is ‘USA’:

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

The OR operator is used to combine two or more conditions, and it returns true if any of the conditions are satisfied. For example, the following query returns all the records from the Customers table where the City is ‘New York’ or the Country is ‘USA’:

SELECT * 
FROM Customers 
WHERE City = 'New York' 
OR Country = 'USA';

It is important to use parentheses when combining multiple conditions using both AND and OR operators to ensure the correct order of evaluation. For example, the following query returns all the records from the Customers table where the City is ‘New York’ and the Country is ‘USA’ or the City is ‘Los Angeles’:

SELECT * 
FROM Customers 
WHERE (City = 'New York' AND Country = 'USA') 
OR City = 'Los Angeles';

Without the parentheses, the query would return all the records from the Customers table where either the City is ‘New York’ and the Country is ‘USA’, or the City is ‘Los Angeles’.

In summary, the AND operator combines conditions that must all be true, while the OR operator combines conditions where at least one must be true. It is important to use parentheses when combining both operators to ensure the correct order of evaluation.