SOQL Cheat Sheet

SOQL (Salesforce Object Query Language) is used to search your Salesforce data for specific information. Here are some commonly used SOQL statements you should keep as an SOQL Cheat Sheet for quick reference.

What is SOQL in Salesforce?

SOQL Stands for Salesforce Object Query Language. It is similar to SQL but designed specifically for Salesforce data. It uses the same structure and keywords as SQL. You can query data from Salesforce using SOQL.

SOQL Cheat Sheet

Here are some statements that you shouldn’t miss:

Tips: Writing SQL Statements

  • SOQL statements are not case-sensitive.
  • SOQL statements can be on one or more lines.
CommandDescriptionExample
SELECTSELECT identifies the columns to be displayed.
Exp: Get data from the Account object.

SELECT AccountNumber, Unique_ID__c, BillingCity
FROM Account
FROMFROM identifies the table containing those columns.SELECT AccountNumber, Name
FROM Account

How to fetch all fields in Salesforce, like Select * From Account?

SELECT FIELDS(ALL) FROM Account LIMIT 200

TYPEOF Working with Polymorphic Relationships in SOQL Queries.

Restricting and Sorting Data

CommandDescriptionExample
WHERERestricts the query to rows that meet a condition

Exp: Get All accounts where the billing city is New York

SELECT Name, BillingCity FROM Account WHERE billingCity = ‘New York’
ORDER BYSorting rows by using the ORDER BY clauseSELECT Name, NumberOfEmployees FROM Account ORDER BY NumberOfEmployees

Conditional Expressions

CommandDescriptionExample
=EqualsExp: Get All accounts where the billing city is New York

SELECT Name, BillingCity FROM Account WHERE billingCity = ‘New York’
!=Not EqualsSELECT Name, BillingCity FROM Account WHERE billingCity != ‘New York’
< , <=Less Than, Less Than or Equal ToExp: Get All accounts AnnualRevenue is less than 100K

SELECT Name, AnnualRevenue FROM Account WHERE AnnualRevenue < 100000
> ,>=Greater Than, Greater Than or Equal ToExp: Get All accounts AnnualRevenue is greater than 100K

SELECT Name, AnnualRevenue FROM Account WHERE AnnualRevenue > 100000
INCLUDES, EXCLUDESIncludes or Excludes Values. Applies only to Multi-Select Picklists.SELECT Id, MSP1__c FROM CustObj__c WHERE MSP1__c includes (‘AAA;BBB’,’CCC’)
LIKEReturns the records where the field values match the pattern specified after the LIKE operatorExp: Get All accounts Where Name starts with A

SELECT Name, Type FROM Account WHERE Name LIKE ‘A%’
INSelects only the records where the value of the field matches any of the values specified after IN keywordExp: Get All accounts Where the billing city is in New York or San Francisco.

SELECT BillingCity FROM Account WHERE BillingCity IN (‘New York’ , ‘San Francisco’ )
NOT INSelect only the records where the value of the DOES NOT MATCH any of the values specified after IN keywordExp: Get All accounts Where the billing city is not in New York or San Francisco.

SELECT BillingCity FROM Account WHERE BillingCity NOT IN (‘New York’ , ‘San Francisco’ )

Like

– The % and _ wildcards .The % wildcard matches zero or more characters.
– The _ wildcard matches exactly one character.

Learn SOQL Best practices.

Logical Operators

Logical operators can be used in the field expression of the WHERE clause in a SOQL query. These operators are AND, OR, and NOT.

CommandDescriptionExample
ANDReturns TRUE if both component conditions are true
ORReturns TRUE if either component condition is true
NOTReturns TRUE if either component condition is false

Aggregate Functions in SOQL

CommandDescriptionExample
COUNT()Count the number of recordsSELECT count() FROM Account
MIN()Returns the minimum value of a fieldSELECT MIN(Amount) FROM Opportunity
MAX()to get the maximum value of a fieldSELECT MAX(Amount) FROM Opportunity
SUM()Returns the total sum of a numeric fieldSELECT SUM(Amount) FROM Opportunity
AVG()it will return the average value of a numeric field.SELECT AVG(Amount) FROM Opportunity

Grouping, Filtering

CommandDescriptionExample
GROUP BYUse the GROUP BY clause to group the result set based on the field specified.Exp: Get a count of accounts based on the Industry.

SELECT Industry, COUNT(Name)
FROM Account GROUP By Industry
HAVINGHAVING is an optional clause used to filter the results that an aggregated function returnsExp: Get a count of accounts based on the Industry and where we have more than one record.

SELECT Industry, COUNT(Name) FROM Account GROUP BY Industry HAVING COUNT(Name) > 1

Learn more about HAVING.

Limit and Offset in SOQL

CommandDescriptionExample
Limitmaximum number of rows to returnExp: Get 100 Account record

SELECT Name FROM Account
LIMIT 100
OffsetYou can display the results in multiple pages by using the OFFSET clause on a SOQL query.SELECT Name, Id FROM Merchandise__c ORDER BY Name OFFSET 100

Date Literals

CommandDescriptionExample
TODAYStarts at 12:00:00 AM on the current day and continues for 24 hours.SELECT Id FROM Account WHERE CreatedDate > TODAY
YESTERDAYDay before the current day and continues for 24 hoursSELECT Id FROM Account WHERE CreatedDate = YESTERDAY
TOMORROWDay after the current day and continues for 24 hoursSELECT Id FROM Account WHERE CreatedDate = TOMORROW
THIS_WEEKfirst day of the current week and continues for seven daysSELECT Id FROM Account WHERE CreatedDate < THIS_WEEK
LAST_90_DAYS90 days before the current day and continues up to the current secondSELECT Id FROM Account WHERE CreatedDate = LAST_90_DAYS
LAST_MONTHthe first day of the current month and continues for all the days of that monthSELECT Id FROM Account WHERE CreatedDate = LAST_MONTH
THIS_YEARJanuary 1 of the current year and continues through the end of December 31 of the current yearSELECT Id FROM Account WHERE CreatedDate = THIS_YEAR

Learn more.

SOQL Crash course

Check the Crash course to learn more about SOQL and relationship queries.

YouTube video

Summary

Learn more about SOQL & DML in Salesforce. Hope this helps! Let me know if you have any other questions.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 463

Leave a Reply

Your email address will not be published. Required fields are marked *