Summary of Salesforce Supported Virtual Internship Program 2025 - Session 12
Summary of "Salesforce Supported Virtual Internship Program 2025 - Session 12"
This session provides an in-depth introduction and practical walkthrough of SOQL (Salesforce Object Query Language), focusing on its syntax, usage, clauses, query types, aggregate functions, relationships, limitations, and practical examples executed in the Salesforce Developer Console.
Main Ideas and Concepts
1. Introduction to SOQL
- SOQL stands for Salesforce Object Query Language.
- It is distinct from SQL and specifically used to query Salesforce databases.
- Used by developers (especially in Apex code) and admins to retrieve and manipulate Salesforce data.
- Can be used without Apex code, e.g., via the Developer Console’s Query Editor.
2. Basic SOQL Syntax
- Basic format:
SELECT <fields> FROM <object> [WHERE <conditions>] [ORDER BY <field> ASC|DESC] [LIMIT <number>]
- Example:
SELECT Id, Name FROM Contact WHERE Name = 'Kaki'
3. Common SOQL Clauses
- SELECT: Specifies fields to retrieve.
- FROM: Specifies the Salesforce object/table.
- WHERE: Filters records based on conditions (supports logical operators like AND, OR, NOT).
- ORDER BY: Sorts results ascending (ASC) or descending (DESC).
- GROUP BY: Aggregates data by specified fields.
- HAVING: Filters aggregated results.
- LIMIT: Limits the number of rows returned.
- OFFSET: Skips a number of rows, useful for pagination.
- WITH: Adds query behavior like enforcing security permissions (
WITH SECURITY_ENFORCED
).
4. Conditional Expressions
=
(equals),!=
(not equals),<
,<=
,>
,>=
IN
/NOT IN
for matching multiple values.LIKE
for pattern matching (supports%
as wildcard).INCLUDES
/EXCLUDES
for multi-select picklist fields.
5. Advanced Clauses
- TYPEOF Clause:
Used for polymorphic relationships (fields referring to multiple object types).
Example:
SELECT TYPEOF What WHEN Account THEN Phone ELSE Name END FROM Event
- Relational Queries:
- Child-to-parent (can traverse up to 5 levels)
- Parent-to-child (one level down, uses subqueries)
- Custom relationship fields end with__r
.
6. Aggregate Queries
- Aggregate functions include:
COUNT()
COUNT_DISTINCT()
AVG()
MIN()
MAX()
SUM()
- Aggregates are used with
GROUP BY
,HAVING
, and can be combined with clauses likeGROUP BY ROLLUP
orGROUP BY CUBE
. - Aggregate queries condense large datasets into summarized results.
7. Limits and Best Practices
- Query length limit: 100,000 characters.
- String length limit: 4,000 characters per string.
- Max 55 child-to-parent relationships per query.
- Max 5 chained parent-to-child relationships.
- Query timeout: 120 seconds; result processing up to 30 minutes.
- Apex limits: max 100 SOQL queries per synchronous transaction, 200 per asynchronous, max 5,000 rows returned.
- Aggregate queries count towards limits differently (each returned row counts as one).
8. Using Developer Console
- Developer Console is the main tool for writing and running SOQL queries and Apex code.
- The Query Editor tab allows running SOQL queries directly.
- Examples demonstrated include simple selects, filters with WHERE, ordering, limits, relational queries, and aggregate queries.
9. Practical Examples and Exercises
- Fetching records with specific conditions (e.g., opportunities closed with amount > 50,000).
- Retrieving contacts belonging to accounts in a specific industry.
- Listing accounts with more than 3 related contacts using
COUNT()
andGROUP BY
. - Getting the latest case for each Contact.
- Querying opportunities where related Account’s billing country is USA and stage is prospecting.
- Fetching accounts with no opportunities using
NOT IN
. - Retrieving contacts with related cases created in the last 30 days.
- Finding opportunities with product totals over $10,000 by querying OpportunityLineItems and grouping by Opportunity ID.
10. Q&A Highlights
- Explanation of
HAVING
clause for filtering aggregated results. - Difference between synchronous (immediate execution) and asynchronous (queued execution) operations.
- Clarification on relationship queries and use of
__r
suffix for custom relationships. - How to fetch unique IDs.
Category
Educational