Step: 4 After entering the query, click the “Execute” button (it looks like a play button) to run the query.
Step 5: The results of your query will be displayed in the Query Editor’s Results tab.
In this example:
SELECT Id, Name, Owner.Name specifies the fields to be retrieved.
FROM Account specifies the object from which to retrieve data (in this case, the Account object).
WHERE Name = ‘MyAccount’ is the optional WHERE clause used to filter records.
ORDER BY Owner.Name is the optional ORDER BY clause used to sort the results.
You can build more complex queries by combining these clauses and using features
like subqueries for related data and aggregate functions for calculations.
SOQL supports aggregate functions like COUNT, SUM, AVG, MAX, and MIN. These functions can be used to perform calculations on groups of records and retrieve summarized data. For example:
SELECT Industry, AVG(AnnualRevenue)
FROM Account
GROUP BY Industry
HAVING AVG(AnnualRevenue) > 1000000
Aggregate functions in SOQL are essential for summarizing and analyzing data within Salesforce objects. They allow you to generate reports and metrics to gain insights into your business data, such as calculating totals, averages, and counts within specific criteria or groups.
SOQL queries respect Salesforce’s security and access control settings like field-level security and permission sets. Users can only query data they have the necessary permissions to access.
The LIMIT function can be used to restrict the number of records returned by a query. For example:
SELECT Name, Industry FROM Account LIMIT 10
This query retrieves the names and industries of the first 10 accounts in the Salesforce database. The LIMIT clause limits the result set to 10 records.
However, SOQL queries are subject to governor limits, such as 100 SOQL queries issued synchronous or 200 queries in an asynchronous transaction, or the maximum number of records returned per transaction is 50,000. In the row limit if you have a query with three parent-child relationships for example, it will still count as one query, but the row limit will be reduced to 2,000. This limit applies to the combined result set of all records retrieved, including records from the primary object and any related objects. Each related object contributes to the overall row count, so if you have multiple related objects in your query, the sum of their rows must not exceed the total query row limit.
When you’re working with parent and child records, you are typically dealing with related objects in Salesforce. Salesforce uses a relationship model to link records from different objects together. Understanding how to query and navigate these relationships is crucial when you need to access data from both parent and child records. Let’s come back to the key concepts related to parent and child records in SOQL. Salesforce objects are related to one another through various types of relationships, such as the following.
•\ Lookup Relationship: This is a simple parent-child relationship where a child record has a reference (lookup) to a parent record. For example, an Opportunity may have a lookup to an Account, making Account the parent object.
•\ Master-Detail Relationship: This is a stricter form of parent-child relationship, where child records (detail) are considered subordinate to the parent record (master). Deleting a master record also deletes its related detail records.
•\ Hierarchical Relationship: This type of relationship is unique to the user object in Salesforce. For example, a user may have a manager as another user.