Basics
SOQL Syntax
SOQL Syntax Basics
SOQL syntax uses SELECT and FROM for querying Salesforce objects.
Introduction to SOQL
Salesforce Object Query Language (SOQL) is used to query data from Salesforce. Much like SQL, it allows you to retrieve data from a database, but specifically from Salesforce objects. The basics of SOQL syntax involve the SELECT and FROM keywords, which form the backbone of a SOQL query.
Basic SOQL Query Structure
A basic SOQL query follows a simple structure:
- SELECT - Specifies the fields to retrieve.
- FROM - Specifies the Salesforce object to query.
Here is a simple example of a SOQL query:
Filtering Results with WHERE
To refine the results of a SOQL query, you can use the WHERE clause to filter records based on specific conditions. This clause works similarly to SQL, allowing for detailed queries.
Example:
Retrieve contacts whose email ends with 'example.com':
Sorting Results with ORDER BY
The ORDER BY clause in a SOQL query is used to sort the results. You can specify the field to sort by and the order, either ascending (ASC) or descending (DESC).
Example:
Retrieve contacts and sort them by Name in ascending order:
Limiting Results with LIMIT
SOQL provides the LIMIT clause to restrict the number of records returned by a query. This is useful for improving performance and ensuring that you only retrieve the necessary amount of data.
Example:
Retrieve only the first 10 contacts:
Combining Clauses for Complex Queries
SOQL allows combining WHERE, ORDER BY, and LIMIT clauses to create complex and precise queries. This helps in retrieving exactly the data you need efficiently.
Example:
Get the first 5 contacts with emails ending in 'example.com' sorted by name: