Newer Version Available

This content describes an older version of this product. View Latest

Query

Executes the specified SOQL query.

If the query results are too large, the response contains the first batch of results and a query identifier in the nextRecordsUrl field of the response. The identifier can be used in an additional request to retrieve the next batch.

URI
/vXX.X/query/?q=SOQL query
For retrieving query performance feedback without executing the query:
/vXX.X/query/?explain=SOQL query
For retrieving query performance feedback on a report or list view:
/vXX.X/query/?explain=report or list view ID
For retrieving additional query results if the initial results are too large:
/vXX.X/query/query identifier
Formats
JSON, XML
HTTP Method
GET
Authentication
Authorization: Bearer token
Parameters
Parameter Description
q A SOQL query. Note that you will need to replace spaces with “+” characters in your query string to create a valid URI. An example query parameter string might look like: “SELECT+Name+FROM+MyObject”. If the SOQL query string is invalid, a MALFORMED_QUERY response is returned.
explain

A SOQL query to get performance feedback on. Use explain instead of q to get a response that details how Salesforce will process your query. You can use this feedback to further optimize your queries. You can also use a report or list view ID in place of the query string to get feedback on how Salesforce will process your report or list view.

The explain parameter is available in API version 30.0 and later.

Using explain with the REST API query resource is a beta feature. There is no support associated with this beta feature. For more information, contact Salesforce.

Note

If the SOQL query string is invalid, a MALFORMED_QUERY response is returned. If the report or list view ID is invalid, an INVALID_ID response is returned.

Response body
For a query using the q parameter, the response contains an array of query result records. For a query using the explain parameter, the response contains one or more query plans that can be used to execute the query, report, or list view. The plans are sorted from most optimal to least optimal. Each plan has the following information:
Name Type Description
cardinality number The estimated number of records the query would return, based on index fields, if any.
fields string[] The index fields used for the query, if the leading operation type is Index, otherwise null.
leadingOperationType string The primary operation type that will be used to optimize the query. This can be one of these values:
  • Index—The query will use an index on the query object.
  • Other—The query will use optimizations internal to Salesforce.
  • Sharing—The query will use an index based on the user’s sharing rules. If there are sharing rules that limit which records are visible to the current user, those rules can be used to optimize the query.
  • TableScan—The query will scan all records for the query object, and won’t use an index.
notes feedback note[] An array of one or more feedback notes. Each note contains:
  • description— A detailed description of an aspect of the optimization. This could include information on optimizations that could not be used, with details on why they weren’t used.
  • fields— An array of one or more fields used for the optimization.
  • tableEnumOrId— The table name for the fields used for the optimization.
This response field is available in API version 33.0 and later.
relativeCost number The cost of this query compared to the SOQL selective query threshold. A value greater than 1.0 means the query won’t be selective. See “More Efficient SOQL Queries” in the Apex Code Developer’s Guide for more information on selective queries.
sobjectCardinality number The approximate count of all records in your organization for the query object.
sobjectType string The name of the query object, such as Merchandise__c.
Example
For an example of making a query and retrieving additional query results using the query identifier, see Execute a SOQL Query.
For an example using the explain parameter to get feedback on a query and a report, see Get Feedback on Query Performance.

For more information on SOQL see the Force.com SOQL and SOSL Reference. For more information on query batch sizes, see Changing the Batch Size in Queries in the SOAP API Developer's Guide.