Combining AND and OR in a query

When two or more rows of your query have set criteria to filter the data, and when at least one of the rows with criteria also uses the Or column, the SQL statement will evaluate the combined criteria as follows:

  1. First, it uses AND to connect all of the conditions in the Criteria column.

  2. Next, it uses AND to connect all of the conditions in the first Or column.

    This step is also repeated for any conditions in the other Or columns.

  3. Finally, it uses OR to connect the conditions from the Criteria column to the conditions from the first Or column.

    This step is also repeated for any conditions in the other Or columns.

In the following example, a record will pass all of the conditions and be acted upon by the query if the following is true:

OR

Instead, you probably wanted to see data for orders where the following is true:

OR

Thus, the query should be written as:

In a second example below, data where the company name contains both "Market" and "Food" and data where the company name contains both "Store" and "Food" will pass all of the conditions and be acted upon by the query.

 

<< Back to Adding criteria for the column