Contents Index Properties of NULL Summarizing, Grouping and Sorting Query Results

ASA SQL User's Guide
  Queries: Selecting Data from a Table
    The WHERE clause: specifying rows

Connecting conditions with logical operators


The logical operators AND, OR, and NOT are used to connect search conditions in WHERE clauses.

Using AND 

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 contact
WHERE first_name = 'Beth'
   AND last_name = 'Purcell'
Using OR 

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 first_name column.

SELECT *
FROM contact
WHERE first_name = 'Beth'
   OR first_name = 'Liz'
Using NOT 

The NOT operator negates the expression that follows it. The following query lists all the contacts who do not live in California:

SELECT *
FROM contact
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 contact
WHERE ( city = 'Lexington'
        OR city = 'Burlington' )
   AND state = 'MA'

Contents Index Properties of NULL Summarizing, Grouping and Sorting Query Results