Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Usage » Queries: Selecting Data from a Table » The WHERE clause: specifying rows

Properties of NULL Next Page

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 Contacts
   WHERE GivenName = 'Beth'
      AND Surname = '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 GivenName column.

SELECT *
   FROM Contacts
   WHERE GivenName = 'Beth'
      OR GivenName = '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 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';