Blog Blog Posts Business Management Process Analysis

Salesforce SOQL

In this blog, we will explore SOQL in detail and understand how it can be leveraged to query Salesforce data. We will learn about:

Want to learn about Salesforce? Watch this video below

“@context”: “”,
“@type”: “VideoObject”,
“name”: “How to Learn Salesforce | What is Salesforce | Salesforce Training | Salesforce Course”,
“description”: “Salesforce SOQL”,
“thumbnailUrl”: “”,
“uploadDate”: “2023-05-24T08:00:00+08:00”,
“publisher”: {
“@type”: “Organization”,
“name”: “Intellipaat Software Solutions Pvt Ltd”,
“logo”: {
“@type”: “ImageObject”,
“url”: “”,
“width”: 124,
“height”: 43
“contentUrl”: “”,
“embedUrl”: “”

What is SOQL and Why Do We Need It?

SOQL stands for Salesforce Object Query Language. It is a SQL-like language that queries records from Salesforce objects like Accounts, Contacts, Opportunities, etc. SOQL allows us to retrieve data that matches specific criteria. 

Some of the key benefits of SOQL are:

SOQL query structure and syntax 

The basic structure of a SOQL query is


For example, to query Account records where the Account Name starts with ‘Acme’, the SOQL query would be:

SELECT Name, BillingCountry 
FROM Account
WHERE Name LIKE 'Acme%'

This will return the Name and BillingCountry of 10 Accounts where the Name starts with ‘Acme’.

Different Parts of a SOQL Query 

Different parts of a SOQL query

SOQL has several parts, some of them are:

Using SOQL to query Salesforce standard and custom objects

We can use SOQL to query both standard Salesforce objects as well as custom objects. The syntax is the same, we just need to specify the correct object name.

For example, to query Accounts, the object name is simply ‘Account’:

SELECT Name, BillingCountry
FROM Account
WHERE Name LIKE 'Acme%' 

To query the Opportunity object, use:

SELECT Id, Name, StageName, Amount
FROM Opportunity

For custom objects, refer to them by their API name. For a custom object with the API name ‘Product__c’, the SOQL would be: 

SELECT Name, Description__c, Price__c
FROM Product__c
WHERE Name = 'Widget'

We can query fields of all data types including text, number, date, lookup relationship, etc. SOQL also allows querying across relationships using dot notation. For example, to retrieve the Account Name for Opportunity records, we can use:

SELECT Opportunity.Name, Account.Name
FROM Opportunity

This works for both standard object relationships as well as custom object relationships.

Want to make a career in the Salesforce industry? Make sure to enroll in a Salesforce Training Course right now!

Limiting results and sorting with SOQL  

To optimize performance, limiting the number of records retrieved by a SOQL query using the LIMIT clause is a good practice. For example, to retrieve only the first 50 Account records, we can use:

FROM Account

To sort the retrieved records in ascending or descending order, use the ORDER BY clause. For example, to sort Accounts by Name in descending order:

FROM Account

We can sort based on both standard and custom fields. To sort on multiple fields, specify them in the desired sort order:

SELECT Id, Name, BillingCountry
FROM Account
ORDER BY Name DESC, BillingCountry ASC

This will first sort records by Name in descending order. For records with the same Name, they will be sorted by BillingCountry in ascending order.

Using SOQL Aggregates to Summarize Data  

Aggregate functions allow us to summarize data and calculate metrics like count, sum, average, etc. Some commonly used aggregate functions in SOQL are:

For example, to get the count of Accounts:

FROM Account

To get the sum of Amount for all Opportunities:

FROM Opportunity  

We can also use GROUP BY to summarize data by a particular field. For example, to get the count of Accounts by BillingCountry:

SELECT BillingCountry, COUNT()
FROM Account
GROUP BY BillingCountry

The HAVING clause is used to filter summarized records. For example, to get the sum of Amount for Opportunities where the sum is greater than $200,000: 

FROM Opportunity
GROUP BY CloseDate
HAVING SUM(Amount) > 200000

SOQL queries with aggregates can also contain standard fields, custom fields and be limited and sorted. They provide a powerful way to analyze and summarize your Salesforce data.

Check out Salesforce Tutorial to learn Salesforce!

Embedding SOQL queries in Apex and Visualforce pages

SOQL queries are typically executed in:

For example, here is an Apex class that executes a SOQL query and iterates over the results:

public class AccountQuery {
  public List queryAccounts(){
    List accounts = [SELECT Id, Name FROM Account LIMIT 10];
    for(Account a : accounts){
    return accounts;

In Visualforce, a SOQL query can be used to populate a table as follows:



is used to execute the Apex class that returns the Query results. These results are then displayed in the Visualforce page using .

Read the Top Salesforce Interview Questions to crack your upcoming interviews!

Comparing SOQL and SOSL

Comparing SOQL and SOSL

SOQL and SOSL have several differences between them. Some of them are:

Differentiation SOQL SOSL
Syntax SOQL uses a SQL-like syntax for querying data from a single object or related objects. SOSL uses a text-based syntax for searching across multiple objects and fields.
Functionality SOQL is best suited for retrieving specific records and related data from a single object or a set of related objects. SOSL is designed for searching across multiple objects and fields, making it ideal for complex search queries.
Query performance SOQL is optimized for retrieving a small number of records and has better performance for retrieving data from a single object. SOSL is optimized for searching across multiple objects and fields and can retrieve a large number of records more efficiently.
Sorting and filtering SOQL supports the sorting and filtering of records based on specific fields. SOSL supports the sorting and filtering of records based on relevance.


Salesforce SOQL is an incredibly powerful tool that allows you to extract and manipulate data from your Salesforce database. With its intuitive syntax and documentation, learning SOQL is easier than you might think. It is a must-know tool for anyone working with Salesforce data. 

Drop your queries at Intellipaat’s Salesforce Community to get them resolved!

The post Salesforce SOQL appeared first on Intellipaat Blog.

Blog: Intellipaat - Blog

Leave a Comment

Get the BPI Web Feed

Using the HTML code below, you can display this Business Process Incubator page content with the current filter and sorting inside your web site for FREE.

Copy/Paste this code in your website html code:

<iframe src="" frameborder="0" scrolling="auto" width="100%" height="700">

Customizing your BPI Web Feed

You can click on the Get the BPI Web Feed link on any of our page to create the best possible feed for your site. Here are a few tips to customize your BPI Web Feed.

Customizing the Content Filter
On any page, you can add filter criteria using the MORE FILTERS interface:

Customizing the Content Filter

Customizing the Content Sorting
Clicking on the sorting options will also change the way your BPI Web Feed will be ordered on your site:

Get the BPI Web Feed

Some integration examples