The following table lists the comparisonOperator values that are used in fieldExpression syntax. Note that comparisons on strings are case-insensitive.
| Operator | Name | Description |
|---|---|---|
| = | Equals | Expression is true if the value in the specified fieldName equals the specified value in the expression. String comparisons using the equals operator are case-insensitive. |
| != | Not equals | Expression is true if the value in the specified fieldName does not equal the specified value. |
| < | Less than | Expression is true if the value in the specified fieldName is less than the specified value. |
| <= | Less or equal | Expression is true if the value in the specified fieldName is less than, or equals, the specified value. |
| > | Greater than | Expression is true if the value in the specified fieldName is greater than the specified value. |
| >= | Greater or equal | Expression is true if the value in the specified fieldName is greater than or equal to the specified value. |
| LIKE | Like | Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator
in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text
strings and includes support for wildcards.
For example, the following query matches Appleton, Apple,
and Bappl , but not Appl: SELECT AccountId, FirstName, lastname FROM Contact WHERE lastname LIKE 'appl_%' |
| IN | IN | If the value equals any one of the specified values in a WHERE clause. For example: Note that the values for IN must be in parentheses. String values must be surrounded by single quotes. IN and NOT IN can also be used for semi-joins and anti-joins when querying on ID (primary key) or reference (foreign key) fields. |
| NOT IN | NOT IN | If the value does not equal any of the specified values in
a WHERE clause. For example: SELECT Name FROM Account
WHERE BillingState NOT IN ('California', 'New York')Note that the values for NOT IN must be in parentheses, and string values must be surrounded by single quotes. There is also a logical operator NOT, which is unrelated to this comparison operator. |
| INCLUDES EXCLUDES | Applies only to multi-select picklists. |
You can query values in a field where another field on the same object has a specified set of values, using IN. For example:
SELECT Name FROM Account
WHERE BillingState IN ('California', 'New York')
In addition, you can create more complex queries by replacing the list of values in the IN or NOT IN clause with a subquery. The subquery can filter by ID (primary key) or reference (foreign key) fields. A semi-join is a subquery on another object in an IN clause to restrict the records returned.An anti-join is a subquery on another object in a NOT IN clause to restrict the records returned.
Sample uses of semi-joins and anti-joins include:
If you filter by an ID field, you can create parent-to-child semi- or anti-joins, such as Account to Contact. If you filter by a reference field, you can also create child-to-child semi- or anti-joins, such as Contact to Opportunity, or child-to-parent semi- or anti-joins, such as Opportunity to Account.
SELECT Id, Name
FROM Account
WHERE Id IN
( SELECT AccountId
FROM Opportunity
WHERE StageName = 'Closed Lost'
)This is a parent-to-child semi-join from Account to Opportunity. Notice that the left operand, Id, of the IN clause is an ID field. The subquery returns a single field of the same type as the field to which it is compared. A full list of restrictions that prevent unnecessary processing is provided at the end of this section.
Notice that the left operand, WhoId, of the IN clause is a reference field. An interesting aspect of this query is that WhoId is a polymorphic reference field as it can point to a contact or a lead. The subquery restricts the results to contacts.
SELECT Id
FROM Opportunity
WHERE AccountId NOT IN
(
SELECT AccountId
FROM Contact
WHERE LeadSource = 'Web'
)
This is a child-to-child anti-join from Opportunity to Contact.
SELECT Id, Name
FROM Account
WHERE Id IN
(
SELECT AccountId
FROM Contact
WHERE LastName LIKE 'apple%'
)
AND Id IN
(
SELECT AccountId
FROM Opportunity
WHERE isClosed = false
)You can use at most two subqueries in a single semi-join or anti-join query. Multiple semi-joins and anti-join queries are also subject to existing limits on subqueries per query.
Because a great deal of processing work is required for semi-join and anti-join queries, salesforce.com imposes the following restrictions to maintain the best possible performance:
The following restrictions apply to the main WHERE clause of a semi-join or anti-join query:
SELECT Id FROM Idea WHERE (Id IN (SELECT ParentId FROM Vote WHERE CreatedDate > LAST_WEEK AND Parent.Type='Idea'))
SELECT Id FROM Contact WHERE Account.Id IN ( SELECT ... )
SELECT Id, Name
FROM Account
WHERE Id IN
(
SELECT AccountId
FROM Contact
WHERE LastName LIKE 'Brown_%'
)
Using Account.Id (dot notation) instead of AccountId is not supported. Similarly, subqueries like Contact.AccountId FROM Case are invalid.
SELECT Id, Name
FROM Account
WHERE Id IN
(
SELECT ParentId
FROM Account
WHERE Name = 'myaccount'
)SELECT Id, Name FROM Account WHERE Parent.Name = 'myaccount'
SELECT Id
FROM Idea
WHERE (Idea.Title LIKE 'Vacation%')
AND (Idea.LastCommentDate > YESTERDAY)
AND (Id IN (SELECT ParentId FROM Vote
WHERE CreatedById = '005x0000000sMgYAAU'
AND Parent.Type='Idea'))The following query is invalid since the nested query is an additional level deep:
SELECT Id
FROM Idea
WHERE
((Idea.Title LIKE 'Vacation%')
AND (CreatedDate > YESTERDAY)
AND (Id IN (SELECT ParentId FROM Vote
WHERE CreatedById = '005x0000000sMgYAAU'
AND Parent.Type='Idea')
)
OR (Idea.Title like 'ExcellentIdea%'))