Boolean Operators

To filter the result set, a query combines several predicates. Boolean operators can help you expand or narrow your query within the where filter. Use the OR function to combine filters. Use the AND function to intersect filters. The NOT function inverts a filter.

The filter types have fields for the AND, OR, and NOT functions. AND and OR take in a list, while NOT takes in a single filter to invert. When combined, the filters allow for arbitrary boolean expressions.

Each filter within the list of filters is joined by the named operator for AND or OR functions.

Let's take a look at a list of filters with the and operator.

Each filter is joined together with the AND SOQL operator.

Here's an example of the previous list of filters.

Similarly, consider the following filters.

The implicit filter with the and operator is translated to a SOQL statement that looks like this.

Whenever two operators have a value on the filter type, the query combines them with an implicit AND operator.

This query is translated to the following SOQL statement. It assumes that Id and Name are included in a SELECT clause.

Similarly, the following query contains multiple operators eq and gt with a value on the filter type.

The previous query is translated to the following SOQL statement. It assumes that Id and Name are included in a SELECT clause.

Similarly, a filter with the or operator looks like this.

The OR function is translated to the following SOQL statement.

Let's take a look at how to use the or filter type.

The NOT function inverts the predicate within the filter.

The previous query is translated to the following SOQL statement with the NOT operator. It assumes that Id and Name are included in a SELECT clause.

The NOT function can be used with the binary functions as well.

The previous NOT function is translated to the following SOQL statement.

This query uses the not filter type with multiple and filters.

The previous query is translated to the following SOQL statement with the WHERE NOT clause.

Futhermore, we can have arbitrary nesting of filters.

The nesting of filter is translated as follows.

This query uses nesting of filters.

The previous query translates to the following SOQL statement.