SaasGuru Logo

🎉Master Salesforce CPQ in 6 Weeks: Streamline Sales and Drive Revenue– Enroll Now! 🎉

🎉Master Salesforce CPQ in 6 Weeks: Streamline Sales and Drive Revenue– Enroll Now! 🎉
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

Salesforce Announces Free AI Certifications & Training for All

As Artificial Intelligence (AI) becomes increasingly vital across industries, acquiring AI skills is essential for professionals looking to stay competitive. To support this growing need, Salesforce is launching free access to their AI Certifications, starting on September 23, 2024. This initiative aims to close the AI skills gap and make AI education more accessible to …

Salesforce Announces Free AI Certifications & Training for All Read More »

Atlas: Transforming Automation With Salesforce’s Agentforce

At Dreamforce 2024, Salesforce introduced Atlas, the advanced AI reasoning engine at the heart of Agentforce. Atlas is designed to automate complex decision-making processes, streamline workflows, and personalize customer interactions. This innovation goes beyond typical AI applications, bringing a level of autonomy and learning that empowers businesses to optimize their operations in real time. What …

Atlas: Transforming Automation With Salesforce’s Agentforce Read More »

Top Dreamforce 2024 Keynote Announcements You Can’t Miss!

Welcome to our Dreamforce 2024 live blog, where we’ll cover the biggest announcements from this year’s event. Here’s what you can expect: Key insights on Salesforce’s Agentforce for autonomous agents. Major AI advancements and a $1B investment in AI technologies. Exciting partnerships with IBM and Google to extend agent capabilities. Data Cloud enhancements for faster, …

Top Dreamforce 2024 Keynote Announcements You Can’t Miss! Read More »