The logical operators AND, OR, and NOT are used to connect search conditions in WHERE clauses.
The AND operator joins two or more conditions and returns results only when all of the conditions are true. For example, the following query finds only the rows in which the contact's last name is Purcell and the contact's first name is Beth. It does not find the row for Beth Glassmann.
SELECT * FROM Contacts WHERE GivenName = 'Beth' AND Surname = 'Purcell';
The OR operator also connects two or more conditions, but it returns results when any of the conditions is true. The following query searches for rows containing variants of Elizabeth in the GivenName column.
SELECT * FROM Contacts WHERE GivenName = 'Beth' OR GivenName = 'Liz';
The NOT operator negates the expression that follows it. The following query lists all the contacts who do not live in California:
SELECT * FROM Contacts WHERE NOT State = 'CA';
When more than one logical operator is used in a statement, AND operators are normally evaluated before OR operators. You can change the order of execution with parentheses. For example:
SELECT * FROM Customers WHERE ( City = 'Newmarket' OR City = 'Forest Hill' ) AND State = 'MN';