SELECT

The SOQL SELECT command uses the following syntax:

SELECT fieldList FROM objectType [WHERE conditionExpression] 
[ORDER BY]
LIMIT ? 
Syntax Description
fieldList subquery?
Specifies a list of one or more fields, separated by commas, that you want to retrieve from the specified object. The bold elements in the following examples are fieldlists:
  • SELECT Id, Name, BillingCity FROM Account
  • SELECT count() FROM Contact
  • SELECT Contact.Firstname, Contact.Account.Name FROM Contact
You must specify valid field names and must have read-level permissions to each specified field. The fieldList defines the ordering of fields in the query results.
fieldList can include a subquery if the query will traverse a relationship. For example:
SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account
Note
See Relationship Queries for more information about the syntax for parent-to-child and child-to-parent relationships represented by the last two examples in the list above.

The fieldlist can also be count() or be wrapped in toLabel().

objectType Specifies the type of object that you want to query(). You must specify a valid object and must have read-level permissions to that object. For a list of valid objects, see Standard Objects.
conditionExpression If WHERE is specified, determines which rows and values in the specified object (objectType) to filter against. If unspecified, the query() retrieves all the rows in the object that are visible to the user. See WHERE for the appropriate syntax.
Note
SOQL statements cannot exceed 10,000 characters. For SOQL statements that exceed this maximum length, the API returns a MALFORMED_QUERY exception code ; no result rows are returned.

Condition Expression (WHERE Clause)

The conditionExpression, the WHERE clause, uses the following syntax:

fieldExpression   logicalOperator   fieldExpression2 ?
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 MailingCity='California'
  • You can use parentheses to define the order in which fieldExpressions are evaluated. For example, the following 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
  • Client applications must specify parentheses when nesting operators. However, multiple operators of the same type do not need to be nested.
Note
The WHERE clause behaves in two different ways, depending on the version, when handling null values in a parent field for a relationship query. In a WHERE clause that checks for a value in a parent field, if the parent does not exist, the record is returned in Version 13.0 and later, but not returned in versions before 13.0.
SELECT Id FROM Case WHERE Contact.Lastname = null
Case record Id values are returned in version 13.0 and later, but are not returned in versions before 13.0. For more information, see Lookup Relationships and Outer Joins.

See fieldExpression for the syntax of fieldExpression. See Logical Operators for the valid logical operators.

fieldExpression

fieldExpression uses the following syntax:

fieldName   comparisonOperator   value

where:

Syntax Description
fieldName The name of a field in the specified object. Use of single or double quotes around the name will 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. It does not need to be a field in the fieldList.
comparisonOperator See Comparison Operators for a list of valid operators.
value A value used to compare with the value in fieldName. You must supply a value whose data type matches the field type of the specified field. You must supply a native value—other field names or calculations are not permitted. For date values, use the formatting listed in Date Formats and Date Literals. If quotes are required (for example, they are not for dates and numbers), use single quotes. Double quotes result in an error.

Comparison Operators

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 SAME operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.
  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes.
  • The LIKE operator is supported for string fields only (see string).
  • The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
  • The LIKE operator in SOQL and SOSL does not currently support escaping of special characters % or _.
  • You should not use the backslash character in a search (except to escape a character), as it is reserved.
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: SELECT Name FROM ACCOUNT WHERE BillingState IN ('California', 'New York')

Note that the values for IN must be in parentheses. String values must be surrounded by single quotes.

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 each item in the list must be surrounded by single quotes.

INCLUDES
EXCLUDES
  Applies only to multi-select picklists. See Querying Multi-Select Picklists.

Logical Operators

The following table lists the logical operator values that are used in fieldExpression syntax:

Operator Syntax Description
AND fieldExpressionX AND fieldExpressionY true if both fieldExpressionX and fieldExpressionY are true.
OR fieldExpressionX OR fieldExpressionY true if either fieldExpressionX or fieldExpressionY is true.

Relationship queries with foreign key values in an OR clause behave differently depending on the version of the API. In a WHERE clause using OR, if the foreign key value in a record is null, the record is returned in Version 13.0 and later, but not returned in versions before 13.0.

SELECT Id FROM Contact WHERE LastName = 'foo' or Account.Name = 'bar'
The contact with no parent account has a last name that meets the criteria, so it is returned in version 13.0 and later.
NOT not fieldExpressionX true if fieldExpressionX is false.

Quoted String Escape Sequences

You can use the following escape sequences with SOQL:

Sequence Meaning
\n New line
\r Carriage return
\t Tab
\b Bell
\f Form feed
\" One double-quote character
\' One single-quote character
\\ Backslash

If you use a backslash character in any other context, an error occurs.

Reserved Characters

Reserved characters, if specified in aSELECT clause as a literal string (between single quotes), must be escaped (preceded by the backslash \ character) in order to be properly interpreted. An error occurs if you do not precede reserved characters with a backslash.

The following characters are reserved:
' (single quote)
\ (backslash)

For example, to query the AccountName field for "Bob's BBQ," use the following SELECT statement:

SELECT Id FROM Account WHERE Name LIKE 'Bob\'s BBQ'

count()

In order to discover the number of rows that are returned by a query, use count() in a SELECT clause:

SELECT count() FROM objectType [WHERE conditionExpression][LIMIT   number_of_rows] 

For example:

     SELECT count() FROM Account WHERE Name LIKE ’a%’
     SELECT count() FROM Contact, Contact.Account WHERE Account.name = ’MyriadPubs’

count() must be alone in the SELECT statement, it cannot be mingled with other elements in the SELECT clause. count() cannot be used with ORDER BY, however, it can be used with LIMIT.

The query result Size field returns the number of rows. The records themselves are returned as null.

ORDER BY

You can use ORDER BY in a SELECT statement:

SELECT fieldList FROM  objectType [WHERE conditionExpression] 
[ORDER BY fieldName ASC | DESC ? NULLS FIRST | LAST ? ]
[LIMIT number_of_rows] 
Syntax Description
ASC or DESC Specifies whether the results are ordered in ascending (ASC) or descending (DESC) order. Default order is ascending.
NULLS FIRST or NULLS LAST Orders null records at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. By default, null values are sorted first.

For example, the following query returns a query result with Account records in alphabetical order by first name, sorted in descending order, with accounts that have null names appearing last:

SELECT Name FROM Account ORDER BY Name DESC NULLS LAST

The following factors affect results returned with ORDER BY:

  • Sorting is case insensitive.
  • ORDER BY is compatible with Relationship Queries syntax.
  • Multiple column sorting is supported, by listing more than one fieldName clause.
  • Relationship queries with foreign key values in an ORDER BY clause behave differently depending on the version of the Force.comAPI. In an ORDER BY clause, if the foreign key value in a record is null, the record is returned in Version 13.0 and later, but not returned in versions before 13.0.
    SELECT Id, CaseNumber, Account.Id, Account.Name FROM Case ORDER BY Account.Name
    Any case record for which AccountId is empty is returned in Version 13.0 and later.

The following limitations apply to data types when using ORDER BY:

  • These data types are not supported: reference, multi-select picklist, and long text area.
  • All other data types are supported, with the following caveats:
    • convertCurrency() always sorts using corporate currency value, if available.
    • phone data does not include any special formatting when sorting, for example, non-numeric characters such as dash or parentheses are included in the sorting.
    • picklist sorting is defined by the picklist sort determined during setup.

You can use ORDER BY with the optional LIMIT qualifier, in a SELECT statement:

     SELECT Name FROM Account WHERE industry = ’media’
          ORDER BY BillingPostalCode ASC NULLS last LIMIT 125

You are limited to 32 fields in an ORDER BY query. If you exceed the limit, a malformed query fault is returned.

LIMIT

Use LIMIT to specify the maximum number of rows to return:

     SELECT fieldList FROM objectType [WHERE conditionExpression] 
       LIMIT number_of_rows   

For example:

     SELECT Name FROM Account WHERE industry = ’media’ LIMIT 125

This query returns the first 125 records whose industry is Media.

You can use LIMIT with count() as the fieldList to count up to the maximum specified.

Example SELECT Clauses

Type of Search Example(s)
Simple query SELECT Id, Name, BillingCity FROM Account
WHERE SELECT Id FROM Contact WHERE Name like 'A%' AND MailingCity='California'
ORDER BY SELECT Name FROM Account ORDER BY Name DESC NULLS LAST
LIMIT SELECT Name FROM Account WHERE industry = ’media’ LIMIT 125
ORDER BY with LIMIT SELECT Name FROM Account WHERE industry = ’media’ ORDER BY BillingPostalCode ASC NULLS last LIMIT 125
count() SELECT count() FROM Contact
Relationship queries: child-to-parent SELECT Contact.Firstname, Contact.Account.Name FROM Contact

SELECT Id, Name, Account.Name FROM Contact WHERE Account.Industry = ’media’

Relationship queries: parent-to-child SELECT Name, (SELECT LastName FROM Contacts) FROM Account

SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account

Relationship query with WHERE SELECT Name, (SELECT lastname FROM Contacts WHERE CreatedBy.Alias = 'x') FROM Account WHERE industry = ’media’
Relationship query: child-to parent with custom objects SELECT Id, FirstName__c, Mother_of_Child__r.FirstName__c FROM Daughter__c WHERE Mother_of_Child__r.LastName__c LIKE 'C%'
Relationship query: parent to child with custom objects SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts) FROM Account WHERE Name like ’Acme%’
Relationship queries with polymorphic key SELECT Id, Owner.Name FROM Task WHERE Owner.FirstName like ’B%’

SELECT Id, Who.FirstName, Who.LastName FROM Task WHERE Owner.FirstName LIKE ’B%’

SELECT Id, What.Name FROM Event

Relationship queries with aggregate SELECT Name, (SELECT CreatedBy.Name FROM Notes) FROM Account

SELECT Amount, Id, Name, (SELECT Quantity, ListPrice, PricebookEntry.UnitPrice, PricebookEntry.Name FROM OpportunityLineItems) FROM Opportunity

© Copyright 2000-2008 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.