Earn a Salesforce AI Certification by IIT + 12-Week Internship Secure Your Spot Today!

Earn a Salesforce AI Certification by IIT + 12-Week Internship Secure Your Spot Today!
SOQL Cheatsheet: Aggregate Functions, Conditional Expressions, Date Literals

SOQL Cheatsheet: Aggregate Functions, Conditional Expressions, Date Literals

SOQL (Salesforce Object Query Language) is a powerful tool for querying data in Salesforce. Whether you’re a developer, administrator, or Salesforce enthusiast, understanding SOQL’s aggregate functions, conditional expressions, and date literals can significantly enhance your data manipulation and reporting capabilities.

This guide provides a handy cheat sheet and explanations to help you get the most out of your SOQL queries.

Aggregate Functions in SOQL

Aggregate functions allow you to perform calculations on your data directly within your SOQL queries. Here are the key aggregate functions you need to know:

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 fieldSELECT AVG(Amount) FROM Opportunity

saasguru salesforce labs

Conditional Expressions in SOQL

Conditional expressions are used to filter records based on specific criteria. Here’s a breakdown of the main conditional expressions:

CommandDescriptionExample
=EqualsSELECT FirstName, LastName FROM Contact WHERE LastName = ‘Smith’
!=Not EqualsSELECT Name, StageName FROM Opportunity WHERE StageName != ‘Closed Won’
<, <=Less Than, Less Than or Equal ToSELECT CaseNumber, CreatedDate FROM Case WHERE CreatedDate < 2023-01-01
>, >=Greater Than, Greater Than or Equal ToSELECT Name, LeadScore FROM Lead WHERE LeadScore > 50
INCLUDES, EXCLUDESIncludes or Excludes Values (Multi-Select Picklists)SELECT Id, CustomField__c FROM CustomObject__c WHERE CustomField__c INCLUDES (‘Option1’)
LIKEReturns records matching a patternSELECT Name FROM Account WHERE Name LIKE ‘%Inc’
INField matches any specified valuesSELECT Name, Type FROM Account WHERE Type IN (‘Customer’, ‘Partner’)
NOT INField does not match specified valuesSELECT Name, Email FROM Contact WHERE Email NOT IN (‘%@gmail.com’, ‘%@yahoo.com’)

Date Literals in SOQL

Date literals simplify working with dates in your SOQL queries. Here’s a list of commonly used date literals:

CommandDescriptionExample
TODAYStarts at 12:00:00 AM and continues for 24 hoursSELECT Id FROM Account WHERE CreatedDate > TODAY
YESTERDAYDay before the current daySELECT Id FROM Account WHERE CreatedDate = YESTERDAY
TOMORROWDay after the current daySELECT 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 daySELECT Id FROM Account WHERE CreatedDate = LAST_90_DAYS
LAST_MONTHThe first day of the previous month and continues for all the days of that monthSELECT Id FROM Account WHERE CreatedDate = LAST_MONTH
THIS_YEARJanuary 1 of the current year through December 31 of the current yearSELECT Id FROM Account WHERE CreatedDate = THIS_YEAR

Also Read – What is Time and DateTime in Salesforce’s APEX

Conclusion

Mastering SOQL requires a good understanding of its functions, expressions, and literals. This cheat sheet is a quick reference to help you write efficient and effective queries. By leveraging aggregate functions, conditional expressions, and date literals, you can perform complex data manipulations and gain valuable insights from your Salesforce data.

Start your Salesforce journey today with saasguru! With a free trial that gives you access to over 30 Salesforce Certification Courses, 50+ Mock Exams, and 50+ Salesforce Labs for practical learning, you’re set for success. 

Begin your training with saasguru and enhance your professional capabilities!

Frequently Asked Questions (FAQs)

1. What is SOQL?

SOQL (Salesforce Object Query Language) is used to query data in Salesforce.

2. What are aggregate functions in SOQL?

Aggregate functions like COUNT(), MIN(), MAX(), SUM(), and AVG() perform calculations on your data.

3. How can I filter records in SOQL?

Use conditional expressions such as =, !=, <, >, LIKE, IN, and NOT IN to filter records.

4. What are date literals in SOQL?

Date literals like TODAY, YESTERDAY, THIS_WEEK, and THIS_YEAR simplify date-based queries.

5. Can I use multiple conditions in one SOQL query?

Yes, you can combine multiple conditions using AND/OR operators.

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Mass Assign User Permission Set in Salesforce

Learn how to efficiently mass assign user permission sets in Salesforce using tools like Data Loader. Save time and ensure accuracy. Read now!

How to Clone a FlexCard in Salesforce

Learn how to clone a FlexCard in Salesforce with this step-by-step guide. Simplify customization and boost efficiency effortlessly!

Salesforce Introduces Agentforce 2.0: Redefining How Work Gets Done

Salesforce introduces Agentforce 2.0, the AI platform transforming digital labor with autonomous agents and seamless workflows. Read now!