In the previous chapter, you gained a solid foundation in crafting basic SOQL queries. Now, let’s take your SOQL skills to the next level by exploring intermediate techniques. In this chapter, we will delve into working with aggregate functions, grouping results, leveraging subqueries and semi-join queries, understanding relationship queries in the child-parent context, and harnessing the power of date and datetime functions.

Working with Aggregate Functions (SUM, AVG, COUNT, etc.)

Aggregate functions allow you to perform calculations on a set of records and return a single value. These functions are essential for performing data analysis and generating insights from your Salesforce data. Let’s take a look at some common aggregate functions and how to use them:


The COUNT function calculates the number of records in a query result. Suppose you want to count the number of opportunities in your org:

In this query, COUNT() returns the total number of opportunities.


The SUM function calculates the sum of a numeric field in a query result. For example, if you want to calculate the total amount of all opportunities:

This query returns the sum of the Amount field across all opportunities.


The AVG function calculates the average value of a numeric field in a query result. To find the average amount of opportunities:

This query returns the average value of the Amount field.


The MAX function retrieves the highest value of a field, while the MIN function retrieves the lowest value. Suppose you want to find the highest and lowest amounts among opportunities:

These queries return the maximum and minimum values of the Amount field, respectively.

Grouping Results with GROUP BY

The GROUP BY clause allows you to group query results based on one or more fields. This is particularly useful when you want to perform aggregate calculations on subsets of data. Let’s consider an example where you want to calculate the total amount of opportunities for each account industry:

In this query:

  • GROUP BY: We group the results based on the Industry field of the related Account object.
  • SUM(Amount): We calculate the total amount of opportunities for each industry.

This query produces a result that shows the total amount for each account industry.

Subqueries and Semi-Join Queries

Subqueries are queries nested within the main query. They allow you to retrieve data based on the results of another query. Subqueries are invaluable for complex scenarios where you need data from one query to filter or influence another query. Let’s explore a common use case: finding contacts associated with opportunities of a certain stage.

In this query:

  • The subquery (SELECT ContactId FROM Opportunity WHERE StageName = 'Closed Won') retrieves the ContactId values of opportunities with the stage ‘Closed Won’.
  • The main query then retrieves the FirstName and LastName of contacts associated with those opportunities.

This is known as a semi-join query, where the main query filters based on the results of the subquery.

Understanding Relationship Queries (Child-Parent)

In this chapter, we’ll focus on child-parent relationship queries. These queries involve accessing fields from related parent objects based on child objects. Imagine you want to retrieve the account name for each contact:

SELECT FirstName, LastName, Account.Name
FROM Contact;

Here, Account.Name accesses the Name field of the related Account object.

Using Date Functions

Date functions are essential for working with date-related data. These functions help you manipulate and analyze date values effectively. Let’s explore a few key functions:


The DAY_ONLY() function extracts the day component from a datetime field. To find contacts created on a specific day:

This query retrieves contacts created on August 1, 2023.

Date Functions:

  • <strong>TODAY</strong>: Represents the current date.
  • <strong>YESTERDAY</strong>: Represents the previous day’s date.
  • <strong>TOMORROW</strong>: Represents the next day’s date.
  • <strong>LAST_N_DAYS:n</strong>: Retrieves records from the last ‘n’ days.
  • <strong>NEXT_N_DAYS:n</strong>: Retrieves records from the next ‘n’ days.
  • <strong>THIS_MONTH</strong>: Represents the current month.

The above query will fetch all opportunities whose close date is today.

DAY_IN_MONTH(): Returns the day of the month for a date.

String Functions

  • <strong>LIKE</strong>: Performs a partial string match.
  • <strong>IN</strong>: Checks if a value is present in a specified list.

UPPER(): Converts a text field to uppercase.


Congratulations! You’ve now ventured into the realm of intermediate SOQL techniques, exploring aggregate functions, grouping results with GROUP BY, mastering subqueries and semi-join queries, understanding child-parent relationship queries, and harnessing the power of date and datetime functions. These techniques expand your data retrieval and analysis capabilities, enabling you to glean deeper insights from your Salesforce data.

As we progress through this guide, you’ll continue to refine your SOQL skills. In the next chapter, we’ll tackle advanced query optimization strategies, focusing on querying large data sets, bulkifying SOQL queries, understanding query execution plans, and practicing indexing best practices. Get ready to elevate your SOQL expertise and optimize your data retrieval processes like a true Salesforce data aficionado!

About the blog

SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:


Subscribe to our email newsletter to be notified when a new post is published.