Basic SOQL Syntax

This is the syntax of a basic SOQL query:

SELECT fields FROM ObjectName [WHERE Condition]

The WHERE clause is optional. Let’s start with a very simple query. For example, the following query retrieves accounts and gets two fields for each account: the ID and the Phone.

SELECT Name,Phone FROM Account

The query has two parts:

  1. SELECT Name,Phone : This part lists the fields that you would like to retrieve. The fields are specified after the SELECT keyword in a comma-delimited list. Or you can specify only one field, in which case no comma is necessary (e.g. ).
  2. FROM Account : This part specifies the standard or custom object that you want to retrieve. In this example, it’s Account. For a custom object called Invoice_Statement, it is Invoice_Statement__c.

Unlike other SQL languages, you can’t specify * for all fields. You must specify every field you want to get explicitly. If you try to access a field you haven’t specified in the SELECT clause, you’ll get an error because the field hasn’t been retrieved.

You don’t need to specify the Id field in the query as it is always returned in Apex queries, whether it is specified in the query or not. For example: SELECT Id,Phone FROM Account  and SELECT Phone FROM Account  are equivalent statements. The only time you may want to specify the Id field if it is the only field you’re retrieving because you have to list at least one field: SELECT Id FROM Account . You may want to specify the Id field also when running a query in the Query Editor as the ID field won’t be displayed unless specified.

Filtering Query Results with Conditions

If you have more than one account in the org, they will all be returned. If you want to limit the accounts returned to accounts that fulfill a certain condition, you can add this condition inside the WHERE clause. The following example retrieves only the accounts whose names are SFDC Computing. Note that comparisons on strings are case-insensitive.

SELECT Name,Phone FROM Account WHERE Name=**'SFDC Computing'**

The WHERE clause can contain multiple conditions that are grouped by using logical operators (AND, OR) and parentheses. For example, this query returns all accounts whose name is SFDC Computing that have more than 25 employees:

SELECT Name,Phone FROM Account WHERE (Name=**'SFDC Computing'** AND NumberOfEmployees>25)

This is another example with a more complex condition. This query returns all SFDC Computing accounts, or all accounts with more than 25 employees whose billing city is Los Angeles.

SELECT Name,Phone FROM Account WHERE (Name=**'SFDC Computing'** OR (NumberOfEmployees>25 AND BillingCity=**'Los Angeles'**))

Instead of using the equal operator (=) for comparison, you can perform fuzzy matches by using the LIKE operator. For example, you can retrieve all accounts whose names start with SFDC by using this condition: WHERE Name LIKE 'SFDC%' . The % wildcard character matches any or no character. The _ character in contrast can be used to match just one character.