WHERE

The condition expression in a WHERE clause of a SOQL query includes one or more field expressions. You can specify multiple field expressions in a condition expression by using logical operators.

Syntax

WHERE conditionExpression

conditionExpression

The conditionExpression uses the following syntax:

fieldExpression [logicalOperator fieldExpression2][...]

Strings in WHERE clauses can’t exceed 4,000 characters. This limit doesn’t apply to SOQL queries in Apex if the WHERE clause includes the IN operator.

Note

The condition expressions in SOQL SELECT statements appear in bold in these examples:

  • SELECT Name FROM Account WHERE Name LIKE 'A%'
  • SELECT Id FROM Contact WHERE Name LIKE 'A%' AND MailingState='California'

You can use date or dateTime values, or date literals. The format for date and dateTime fields are different.

  • SELECT Name FROM Account WHERE CreatedDate > 2011-04-26T10:00:00-08:00
  • SELECT Amount FROM Opportunity WHERE CALENDAR_YEAR(CreatedDate) = 2011

For information on date functions, such as CALENDAR_YEAR(), see Date Functions.

You can use the boolean values TRUE and FALSE in SOQL queries. To filter on a boolean field, use the following syntax:

WHERE BooleanField = TRUE 

WHERE BooleanField = FALSE

fieldExpression

The field expression syntax of the WHERE clause in a SOQL query consists of a field name, a comparison operator, and a value. The query uses these components to compare the field name value to records being searched.

fieldName comparisonOperator value
Syntax Description
fieldName The name of a field for the specified object. Use of single or double quotes around the name result in an error. You must have at least read-level permissions to the field. It can be any field except a long text area field, encrypted data field, or base64-encoded field. The name doesn’t need to be a field in the fieldList.
comparisonOperator Operators that compare values, such as =,<=, IN, and LIKE. Operators are case insensitive for most fields, but case sensitive for case-sensitive fields.
value A value used to compare with the value in the fieldName. Supply a value whose data type matches the field type of the specified field. The value must be a valid value, not other field names or calculations. If quotes are required, use single quotes. Double quotes result in an error. Quotes are unnecessary for dates and numbers.
You can use parentheses to define the order in which fieldExpressions are evaluated. You must specify parentheses when nesting operators. However, multiple operators of the same type don’t need to be nested. In the following example, the expression is true if fieldExpression1 is true and either fieldExpression2 or fieldExpression3 are true.
fieldExpression1 AND (fieldExpression2 OR fieldExpression3)
However, the following expression is true if either fieldExpression3 is true or both fieldExpression1 and fieldExpression2 are true.
(fieldExpression1 AND fieldExpression2) OR fieldExpression3