How to group conditions
This article explains the concept of how to group conditions using the logical operators "And" and "Or." Grouping conditions enables you to design a query that makes multiple comparisons of the data. An understanding of the concept of grouping conditions and the examples presented in this article can help you fine-tune your queries and make them more efficient at finding the exact data you need to answer your questions.
Sections in this article
The And and Or Operators: a Review
How to group conditions using And and Or
The And and Or Operators: a Review
In the where clause, the and/or operators are used to combine multiple conditions to narrow the scope of the query so that you can find the records you are looking for.
The And Operator
The and operator lets you test for two or more conditions. Previously, we looked at queries like the following in which the where clause defines multiple conditions using the and operator:
To review, the and operator allows you to specify as many conditions as you need to locate the records you are looking for. However, it must be noted that all conditions separated by and must be true for a record to be included in the result set. The and operator is restrictive and therefore limits your search. In the above example, for a record to be retrieved from the database it must satisfy each one of these conditions:
● Between the ages of 18 and 35
AND
● Male
AND
● Unmarried
The Or Operator
The or operator also lets you test for two or more conditions. The following illustration shows a where clause in which multiple conditions are joined by or:
Like and, the or operator allows you to specify as many conditions as you need to locate the records you are looking for. However, the or operator is considerably different from and in that it requires that only one of the specified conditions be true for a record to be included in the result set. The or operator is more inclusive than "and," and typically broadens a search. In the previous example, the result set includes records that meet at least one of the following conditions (although more than one or all can be returned):
● Between the ages of 18 and 35
OR
● Male
OR
● Unmarried
Updated