SOQL Comparison Operators
The most straightforward approach to filter data is by using comparison operators.
The examples used in this section refers to the data model we introduced earlier.
Equal to =
This operator returns True when the value in the field matches the ‘value’ specified. For Strings, the match is case sensitive.
Example: Find all the clinics in Florida
Not Equal to !=
This operator returns True when the value in the field does not match the ‘value’ specified.
Example: Find all the patients who do not have their email id registered
Note that null or Null (case insensitive) is a special value to denote a field without any value in SOQL.
Greater than >
This operator returns True when the value in this field is greater than the ‘value’ specified in filter_expression.
Example: Find clinics which accept more than 100 appointments a day
Less than <
This operator returns True when the value in this field is less than the ‘value’ specified in filter_expression.
Example: Find all the patients with age more than 65 years.
Greater than or equal to =<
This operator returns True when the value in this field is greater than or equal to the ‘value’ specified in filter_expression.
Example: Find clinics which accept 100 or more appointments a day
Greater than or equal to <=
This operator returns True when the value in this field is less than or equal to the ‘value’ specified in filter_expression. Example: Find clinics that accept up to 10 appointments a day.
SOQL LIKE
operator
LIKE
operator is similar to the SQL like operator. It is used to match a substring in the field’s values. It returns True if the string specified in the ‘value’ is present as a substring in the field’s value. The value specified can also contain special wildcard characters.
- The
%
wildcard denotes one or more characters. - The
_
wildcard denotes a single character. - The
%
and_
can be matched with an escape character (\% , _). - The
LIKE
operator can only work with string values. - The search is case-insensitive, which is unlike String ‘equals to’ matching.
- The like operator is a complex operator and should be used wisely, as it can put a lot of load on the server.
Example #1: Find all patients whose MRN begins with 70. They denote patients in Florida.
Example #2: Find all patients whose first name has a second letter equal to ‘u’.
SOQL IN
operator
The IN
operator allows you to match a field’s value to multiple specific values in a single filter expression. The multiple values are written inside parentheses, each value enclosed in single quotes and separated by commas.
It returns True if any one of the specified values matches the field’s value. IN operator works only on strings and is case sensitive.
Example: Find all the clinics in New York, Washington and Denver.
SOQL NOT IN
operator
The NOT IN
operator, on the other hand, returns True if the value in the field does not match any of the specified values.
Example: Find all clinics which are not in the megacities New York, Washington and Denver.
IN
clause. The only limit would be that you cannot exceed the max size of the SOQL query (20,000 characters)
SOQL IN operator with subqueries
In the previous section, we used the IN
operator to match a field’s value with a list of values specified in brackets.
The IN
operator can also be used with a subquery to match a field’s value with a list of values present in another query.
Example #1: Find all the clinics which had at least one appointment in the previous month.
Here only those clinics will be returned whose ID we can also find on the Clinic__c field in a list of appointments made in the previous month.
Similarly, we can apply the NOT
operator before IN
:
Example #2: Find all the clinics which did not have any appointments in the previous month
IN
clause with a subquery, only an ID field or a Relationship field, can be compared from the subqueries object. Otherwise, an SOQL error is thrown.