Multiple search conditions within a WHERE clause

 

You can qualify your request further by coding a search condition that includes several predicates.

The search condition you specify can contain any of the comparison operators or the predicates BETWEEN, DISTINCT, IN, LIKE, EXISTS, IS NULL, and IS NOT NULL.

You can combine any two predicates with AND and OR. In addition, you can use the NOT keyword to specify that the search condition that you want is the negated value of the specified search condition. A WHERE clause can have as many predicates as you want.

When SQL evaluates search conditions that contain these connectors, it does so in a specific order. SQL first evaluates the NOT clauses, next evaluates the AND clauses, and then the OR clauses.

You can change the order of evaluation by using parentheses. The search conditions enclosed in parentheses are evaluated first. For example, to select all employees in departments E11 and E21 who have education levels greater than 12, you can specify:

WHERE EDLEVEL > 12 AND
     (WORKDEPT = 'E11' OR WORKDEPT = 'E21')

The parentheses determine the meaning of the search condition. In this example, you want all rows that have a:

If you did not use parentheses:

WHERE EDLEVEL > 12 AND WORKDEPT = 'E11'
    OR WORKDEPT = 'E21'

Your result is different. The selected rows are rows that have:

If you are combining multiple equal comparisons, you can write the predicate with the ANDs as shown in the following example:

WHERE WORKDEPT = 'E11' AND EDLEVEL = 12 AND JOB = 'CLERK'

You can also compare two lists, for example:

WHERE (WORKDEPT, EDLEVEL, JOB) = ('E11', 12, 'CLERK')

When two lists are used, the first item in the first list is compared to the first item in the second list, and so on through both lists. Thus, each list must contain the same number of entries. Using lists is identical to writing the query with AND. Lists can only be used with the equal and not equal comparison operators.

 

Parent topic:

Defining complex search conditions

 

Related reference


Specifying a search condition using the WHERE clause