logo
shape

SOQL

SOQL (Salesforce Object Query Language) statements allow users to search for data using specific filters, such as specific fields or related objects.

SOQL – Salesforce Object Query Language
Utilizing Aggregate Functions in SOQL
This trick is about using the COUNT aggregate function in SOQL to count the number of records that match a certain criteria.

SELECT COUNT(Id) FROM Contact WHERE LastName = 'Watts'
                    
Filtering with SOQL's Date Literals
This trick showcases how to use date literals in SOQL to retrieve records based on the CreatedDate field from the last 30 days.

SELECT Id, Name FROM Account WHERE CreatedDate = LAST_N_DAYS:30
                                            
Querying Child-to-Parent Relationships in SOQL
This trick is about querying child-to-parent relationships in SOQL, which allows you to traverse from a child object to its parent object. Here, it retrieves the associated Account's Name for each Contact.

SELECT Id, Name, Account.Name FROM Contact
                            
Sorting Results with the ORDER BY Clause in SOQL
This trick highlights how to use the ORDER BY clause in SOQL to sort the results of a query. Here, it sorts the Accounts by Name in ascending order.

SELECT Id, Name FROM Account ORDER BY Name ASC
                                
Most Asked Questions about SOQL
Can SOQL perform write operations like INSERT, UPDATE, or DELETE?

No, unlike SQL, SOQL is a query-only language. This means that you can only use it to retrieve data. If you need to insert, update, or delete records, you need to use Data Manipulation Language (DML) operations provided by Apex, Salesforce's proprietary programming language.

How does SOQL handle relationships between objects?

SOQL has a unique way of handling relationships between objects using Relationship Queries. These allow you to retrieve records from one object, then query related objects in a single statement using a nested query. For example, to retrieve an account's related contact records, you would use a nested SOQL query such as:

SELECT Name, (SELECT LastName FROM Contacts) FROM Account

Are there any restrictions or limitations with SOQL queries?

Yes, Salesforce enforces several governor limits on SOQL queries to ensure resources are shared equitably. As of the year 2023, a single SOQL query can retrieve up to 50,000 records, and a transaction can run up to 100 SOQL queries. Additionally, a single query can't select more than 35 child-to-parent relationships and can traverse no more than five levels of parent-to-child relationships.

The total number of SOQL queries in a transaction varies based on whether the Apex code is synchronous or asynchronous. Always refer to the latest Salesforce documentation for the most accurate information.

© All rights Reserved. 2023