List of Frequently used SOQL Queries.
1. What is SOQL?
The Salesforce Object Query Language (SOQL) is used to search your Salesforce data for specific information. SOQL is similar to the Structured Query Language (SQL) but is designed specifically for Salesforce data.
SQL Select Statement
SELECT one or more fields
FROM an object
WHERE filter statements and, optionally, results are ordered
Example:
SELECT Id, Name
FROM Account
WHERE Name = 'DYDC'
2. When to use SOQL?
You can use SOQL when you want to:
- Retrieve data from a single object or from multiple objects that are related to one another
- Count the number of records that meet specified criteria
- Sort results as part of the query
- Retrieve data from number, date, or checkbox fields
3. SQL Select Syntax
SOQL query syntax consists of a required SELECT statement followed by one or more optional clauses, such as TYPEOF, WHERE, WITH, GROUP BY, and ORDER BY.
SELECT fieldList [subquery][...]
[TYPEOF typeOfField whenExpression[...] elseExpression END][...] FROM objectType[,...]
[USING SCOPE filterScope]
[WHERE conditionExpression]
[WITH [DATA CATEGORY] filteringExpression]
[GROUP BY {fieldGroupByList|ROLLUP (fieldSubtotalGroupByList)|CUBE (fieldSubtotalGroupByList)}
[HAVING havingConditionExpression] ]
[ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]
[LIMIT numberOfRowsToReturn]
[OFFSET numberOfRowsToSkip]
[FOR {VIEW | REFERENCE}[,...] ]
[ UPDATE {TRACKING|VIEWSTAT}[,...] ]
4. Frequently Used SOQL Queries
Query Multi-Select Picklists
Filter values where Telecom or Retail is Selected
SELECT Id, Name FROM Account WHERE Industry__c includes ('Telecom','Retail')
Filter Values where both Telecom and Retail are Selected
SELECT Id, Name FROM Account WHERE Industry__c includes ('Telecom;Retail')
Get RecordType Id
Get Id and Name of Account RecordTypes
SELECT Id, Name FROM RecordType WHERE sObjectType = 'Account'
You can also get RecordType Id in Apex from Schema. Example: Below code will give Id of Customer RecordType of Account Object
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Customer').getRecordTypeId();
Get Queue Id
Get Id of Queue with Name ‘Priority’ and Developer Name ‘Priority_Queue’
SELECT Id FROM Group WHERE Type = 'Queue' AND Name = 'Priority'
or
SELECT Id FROM Group WHERE Type = 'Queue' AND DeveloperName = 'Priority_Queue'
Get Public Group Id
Get Id of Public Group with Name ‘CA Admins’ and Developer Name ‘CA_Admins’
SELECT Id FROM Group WHERE Type = 'Group' AND Name = 'CA Admins'
or
SELECT Id FROM Group WHERE Type = 'Group' AND DeveloperName = 'CA_Admins'
Use LIKE
The LIKE operator in SOQL and SOSL 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
Get all Accounts with Name starting with DY
SELECT Id, Name FROM Account WHERE Name LIKE 'DY%'
Use Date Literals
Select all Account created yesterday
SELECT Id FROM Account WHERE
CreatedDate = YESTERDAY
Select all Account created in last 90 days
SELECT Id FROM Account WHERE
CreatedDate = LAST_90_DAYS
// This includes current day
Select all Opportunities closing in next 15 days
SELECT Id FROM Opportunity WHERE
CloseDate > NEXT_N_DAYS:15
// This does not include current day
Query Code Coverage
SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY ApexClassOrTrigger.Name ASC
Additional Information
This article will be updated soon with more frequently used SOQL Queries!