SlideShare a Scribd company logo
1 of 26
Path to Code
Begin Your Salesforce Coding Adventure
Episode 4
Basics of SOQL & Best Practices
Salesforce MVP, Founder of ApexHours
Follow me at @Amit_SFDC @ApexHours
Amit
Chaudhary
Agenda
• What is SOQL
• Basic of SOQL
• Condition Expression Syntax (WHERE Clause)
• Operators , Date Literals
• Aggregate Function
• GROUP BY
• HAVING
• Order By
• LIMIT
• Workbench
• Relationship Queries
• SOQL in Apex , Querying Record in Batches By Using SOQL For Loops
• Basic of SOSL ,Best Practices
Some House Rules
• Mute your mic
• Keep adding questions in Zoom Q&A Window
• No question is too small
• Questions will be answered in last 15 mins
What is SOQL
SOQL means Salesforce Object Query Language which is used to query the records
from the database based on the requirement
Basic Of SOQL
SELECT fieldList [subquery]
FROM objectType
WHERE conditionExpression
[GROUP BY ]
[HAVING havingConditionExpression]
[ORDER BY ASC|DESC]
[LIMIT numberOfRowsToReturn]
Condition Expression Syntax (WHERE Clause)
• SELECT identifies what columns
• FROM identifies which table
• Restrict the rows returned by using the WHERE clause
SELECT |{[DISTINCT] column|expression
[alias],...}
FROM ObjectName
WHERE Condition;
Operators
Comparison Operators
(=,!=,<,<=,>,>=,LIKE,IN,NOT
IN, INCLUDES , EXCLUDES)
Logical Operators
(AND , OR, NOT)
•Comparison Operators
•(=,!=,<,<=,>,>=,LIKE,IN,NOT IN, INCLUDES ,
EXCLUDES)
•Logical Operators
•(AND , OR, NOT)
In Condition
• Use the IN membership condition to test for values in
a list.
SELECT BillingCity FROM Account where BillingCity in ('Singapore','Paris','Portland')
Using the LIKE Condition
• Use the LIKE condition to perform wildcard searches of valid search
string values.
• Search conditions can contain either literal characters or numbers:
• % denotes zero or many characters.
• _ denotes one character.
SELECT BillingCity FROM Account where name like ‘%IBM%’
Date Literals
ORDER BY Clause
• Sort rows with the ORDER BY clause
• ASC: ascending order, default
• DESC: descending order
• The ORDER BY clause comes last in the SELECT statement.
SELECT BillingCity FROM Account order by Name
GROUP BY
• You can use the GROUP BY option in a SOQL query to avoid iterating
through individual query results. That is, you specify a group of
records instead of processing many individual records.
SELECT leadSource FROM Lead Group By LeadSource
Aggregate Function
Function Example
AVG SELECT CampaignId , AVG(Amount) FROM Opportunity GROUP BY CampaignId
Count SELECT COUNT() FROM Account WHERE Name LIKE 'a%'
Count_Distinct SELECT COUNT_DISTINCT(Company) FROM Lead
MAX SELECT MIN(CreatedDate), FirstName, LastName FROM Contact GROUP BY
FirstName, LastName
MIN SELECT Name, MAX(BudgetedCost) FROM Campaign GROUP BY Name
SUM SELECT SUM(Amount) FROM Opportunity WHERE IsClosed = false AND Probability
> 60
Order by and Limit
• Sort rows with the ORDER BY clause
• ASC: ascending order, default
• DESC: descending order
• The ORDER BY clause comes last in the SELECT statement.
SELECT name from Account Order by Name limit 10
Click to edit
Relationship
Queries
For child-to-parent relationships
• Query child-to-parent relationships, which are often many-to-one.
Specify these relationships directly in the SELECT, FROM,
or WHERE clauses using the dot (.) operator.
SELECT Contact.FirstName,Contact.Account.Name from Contact
For parent-to-child relationships
• Specify these relationships using a subquery (enclosed in
parentheses), where the initial member of the FROM clause in the
subquery is related to the initial member of the outer
query FROM clause
• When you use a relationship name in a query, you must use the
relationship names without the __c. Instead, append
an __r (underscore underscore r).
SELECT Name , ( SELECT Contact.FirstName,Contact.LastName FROM Contacts ) From
Account
Click to editSOQL in Apex
Querying Record in Batches By Using SOQL
For Loops
• SOQL for loops iterate over all of the sObject records returned by a SOQL query
• SOQL for loops can process records one at a time using a single sObject variable, or in batches of 200 sObjects at a time
using an sObject list
insert new Account[]{new Account(Name = 'yyy'), new Account(Name = 'yyy'), new Account(Name = 'yyy')};
Integer i = 0;
for (Account tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) {
i++;
}
System.assert(i == 3);
i = 0;
Integer j;
for (Account[] tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) {
j = tmp.size();
i++;
}
System.assert(j == 3);
System.assert(i == 1);
What is SOSL
To perform text-based queries across multiple sObjects, you can use SOSL
(Salesforce Object Search Language)
SOSL
• FIND Clause with Search Term
• IN Clause :- search group
• RETURNING :- which data to return
• When you use a relationship name in a query, you must use the
relationship names without the __c. Instead, append
an __r (underscore underscore r).
FIND ‘PathToCode’ IN ALL FIELDS RETURNING Account(Name), Contact(LastName,
FirstName, Email)
Q & A
Thank You
Subscribe

More Related Content

What's hot

What's hot (20)

Introduction to Apex for Developers
Introduction to Apex for DevelopersIntroduction to Apex for Developers
Introduction to Apex for Developers
 
Salesforce Basic Development
Salesforce Basic DevelopmentSalesforce Basic Development
Salesforce Basic Development
 
Apex code (Salesforce)
Apex code (Salesforce)Apex code (Salesforce)
Apex code (Salesforce)
 
Batch Apex in Salesforce
Batch Apex in SalesforceBatch Apex in Salesforce
Batch Apex in Salesforce
 
Apex Design Patterns
Apex Design PatternsApex Design Patterns
Apex Design Patterns
 
Apex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong FoundationsApex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong Foundations
 
Introduction to the Salesforce Security Model
Introduction to the Salesforce Security ModelIntroduction to the Salesforce Security Model
Introduction to the Salesforce Security Model
 
Apex Design Patterns
Apex Design PatternsApex Design Patterns
Apex Design Patterns
 
Apex Trigger in Salesforce
Apex Trigger in SalesforceApex Trigger in Salesforce
Apex Trigger in Salesforce
 
Episode 20 - Trigger Frameworks in Salesforce
Episode 20 - Trigger Frameworks in SalesforceEpisode 20 - Trigger Frameworks in Salesforce
Episode 20 - Trigger Frameworks in Salesforce
 
Flow in Salesforce
Flow in SalesforceFlow in Salesforce
Flow in Salesforce
 
Data model in salesforce
Data model in salesforceData model in salesforce
Data model in salesforce
 
Building strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsBuilding strong foundations apex enterprise patterns
Building strong foundations apex enterprise patterns
 
Tối ưu-cau-lệnh-oracle-sql
Tối ưu-cau-lệnh-oracle-sqlTối ưu-cau-lệnh-oracle-sql
Tối ưu-cau-lệnh-oracle-sql
 
Salesforce sharing and visibility Part 1
Salesforce sharing and visibility Part 1Salesforce sharing and visibility Part 1
Salesforce sharing and visibility Part 1
 
Salesforce Development Best Practices
Salesforce Development Best PracticesSalesforce Development Best Practices
Salesforce Development Best Practices
 
Lwc presentation
Lwc presentationLwc presentation
Lwc presentation
 
Introduction to Apex Triggers
Introduction to Apex TriggersIntroduction to Apex Triggers
Introduction to Apex Triggers
 
Introduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic MigrationsIntroduction to SQLAlchemy and Alembic Migrations
Introduction to SQLAlchemy and Alembic Migrations
 
Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Database Connection Pooling With c3p0
Database Connection Pooling With c3p0
 

Similar to Episode 4 - Introduction to SOQL in Salesforce

Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
Lucidworks
 

Similar to Episode 4 - Introduction to SOQL in Salesforce (20)

SFDC Advanced Apex
SFDC Advanced Apex SFDC Advanced Apex
SFDC Advanced Apex
 
Sql server 2016 queries
Sql server 2016 queriesSql server 2016 queries
Sql server 2016 queries
 
SQL
SQLSQL
SQL
 
Mentor Your Indexes
Mentor Your IndexesMentor Your Indexes
Mentor Your Indexes
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Access 04
Access 04Access 04
Access 04
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data Scientists
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
ORM - Ivan Marković
ORM - Ivan MarkovićORM - Ivan Marković
ORM - Ivan Marković
 
MySQL basics
MySQL basicsMySQL basics
MySQL basics
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 
BITM3730Week15.pptx
BITM3730Week15.pptxBITM3730Week15.pptx
BITM3730Week15.pptx
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniques
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
 

More from Jitendra Zaa

More from Jitendra Zaa (20)

Episode 13 - Advanced Apex Triggers
Episode 13 - Advanced Apex TriggersEpisode 13 - Advanced Apex Triggers
Episode 13 - Advanced Apex Triggers
 
Episode 18 - Asynchronous Apex
Episode 18 - Asynchronous ApexEpisode 18 - Asynchronous Apex
Episode 18 - Asynchronous Apex
 
Episode 15 - Basics of Javascript
Episode 15 - Basics of JavascriptEpisode 15 - Basics of Javascript
Episode 15 - Basics of Javascript
 
Episode 23 - Design Pattern 3
Episode 23 - Design Pattern 3Episode 23 - Design Pattern 3
Episode 23 - Design Pattern 3
 
Episode 24 - Live Q&A for getting started with Salesforce
Episode 24 - Live Q&A for getting started with SalesforceEpisode 24 - Live Q&A for getting started with Salesforce
Episode 24 - Live Q&A for getting started with Salesforce
 
Episode 22 - Design Pattern 2
Episode 22 - Design Pattern 2Episode 22 - Design Pattern 2
Episode 22 - Design Pattern 2
 
Episode 21 - Design Pattern 1
Episode 21 - Design Pattern 1Episode 21 - Design Pattern 1
Episode 21 - Design Pattern 1
 
Episode 19 - Asynchronous Apex - Batch apex & schedulers
Episode 19 - Asynchronous Apex - Batch apex & schedulersEpisode 19 - Asynchronous Apex - Batch apex & schedulers
Episode 19 - Asynchronous Apex - Batch apex & schedulers
 
Episode 17 - Handling Events in Lightning Web Component
Episode 17 - Handling Events in Lightning Web ComponentEpisode 17 - Handling Events in Lightning Web Component
Episode 17 - Handling Events in Lightning Web Component
 
Episode 16 - Introduction to LWC
Episode 16 - Introduction to LWCEpisode 16 - Introduction to LWC
Episode 16 - Introduction to LWC
 
Introduction to mulesoft - Alpharetta Developer Group Meet
Introduction to mulesoft - Alpharetta Developer Group MeetIntroduction to mulesoft - Alpharetta Developer Group Meet
Introduction to mulesoft - Alpharetta Developer Group Meet
 
Episode 12 - Basics of Trigger
Episode 12 - Basics of TriggerEpisode 12 - Basics of Trigger
Episode 12 - Basics of Trigger
 
Episode 11 building & exposing rest api in salesforce v1.0
Episode 11   building & exposing rest api in salesforce v1.0Episode 11   building & exposing rest api in salesforce v1.0
Episode 11 building & exposing rest api in salesforce v1.0
 
Episode 10 - External Services in Salesforce
Episode 10 - External Services in SalesforceEpisode 10 - External Services in Salesforce
Episode 10 - External Services in Salesforce
 
Episode 14 - Basics of HTML for Salesforce
Episode 14 - Basics of HTML for SalesforceEpisode 14 - Basics of HTML for Salesforce
Episode 14 - Basics of HTML for Salesforce
 
South East Dreamin 2019
South East Dreamin 2019South East Dreamin 2019
South East Dreamin 2019
 
Episode 9 - Building soap integrations in salesforce
Episode 9 - Building soap integrations  in salesforceEpisode 9 - Building soap integrations  in salesforce
Episode 9 - Building soap integrations in salesforce
 
Episode 8 - Path To Code - Integrate Salesforce with external system using R...
Episode 8  - Path To Code - Integrate Salesforce with external system using R...Episode 8  - Path To Code - Integrate Salesforce with external system using R...
Episode 8 - Path To Code - Integrate Salesforce with external system using R...
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
 
Lightning Web Component in Salesforce
Lightning Web Component in SalesforceLightning Web Component in Salesforce
Lightning Web Component in Salesforce
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Recently uploaded (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Episode 4 - Introduction to SOQL in Salesforce

  • 1. Path to Code Begin Your Salesforce Coding Adventure
  • 2. Episode 4 Basics of SOQL & Best Practices
  • 3. Salesforce MVP, Founder of ApexHours Follow me at @Amit_SFDC @ApexHours Amit Chaudhary
  • 4. Agenda • What is SOQL • Basic of SOQL • Condition Expression Syntax (WHERE Clause) • Operators , Date Literals • Aggregate Function • GROUP BY • HAVING • Order By • LIMIT • Workbench • Relationship Queries • SOQL in Apex , Querying Record in Batches By Using SOQL For Loops • Basic of SOSL ,Best Practices
  • 5. Some House Rules • Mute your mic • Keep adding questions in Zoom Q&A Window • No question is too small • Questions will be answered in last 15 mins
  • 6. What is SOQL SOQL means Salesforce Object Query Language which is used to query the records from the database based on the requirement
  • 7. Basic Of SOQL SELECT fieldList [subquery] FROM objectType WHERE conditionExpression [GROUP BY ] [HAVING havingConditionExpression] [ORDER BY ASC|DESC] [LIMIT numberOfRowsToReturn]
  • 8. Condition Expression Syntax (WHERE Clause) • SELECT identifies what columns • FROM identifies which table • Restrict the rows returned by using the WHERE clause SELECT |{[DISTINCT] column|expression [alias],...} FROM ObjectName WHERE Condition;
  • 9. Operators Comparison Operators (=,!=,<,<=,>,>=,LIKE,IN,NOT IN, INCLUDES , EXCLUDES) Logical Operators (AND , OR, NOT) •Comparison Operators •(=,!=,<,<=,>,>=,LIKE,IN,NOT IN, INCLUDES , EXCLUDES) •Logical Operators •(AND , OR, NOT)
  • 10. In Condition • Use the IN membership condition to test for values in a list. SELECT BillingCity FROM Account where BillingCity in ('Singapore','Paris','Portland')
  • 11. Using the LIKE Condition • Use the LIKE condition to perform wildcard searches of valid search string values. • Search conditions can contain either literal characters or numbers: • % denotes zero or many characters. • _ denotes one character. SELECT BillingCity FROM Account where name like ‘%IBM%’
  • 13. ORDER BY Clause • Sort rows with the ORDER BY clause • ASC: ascending order, default • DESC: descending order • The ORDER BY clause comes last in the SELECT statement. SELECT BillingCity FROM Account order by Name
  • 14. GROUP BY • You can use the GROUP BY option in a SOQL query to avoid iterating through individual query results. That is, you specify a group of records instead of processing many individual records. SELECT leadSource FROM Lead Group By LeadSource
  • 15. Aggregate Function Function Example AVG SELECT CampaignId , AVG(Amount) FROM Opportunity GROUP BY CampaignId Count SELECT COUNT() FROM Account WHERE Name LIKE 'a%' Count_Distinct SELECT COUNT_DISTINCT(Company) FROM Lead MAX SELECT MIN(CreatedDate), FirstName, LastName FROM Contact GROUP BY FirstName, LastName MIN SELECT Name, MAX(BudgetedCost) FROM Campaign GROUP BY Name SUM SELECT SUM(Amount) FROM Opportunity WHERE IsClosed = false AND Probability > 60
  • 16. Order by and Limit • Sort rows with the ORDER BY clause • ASC: ascending order, default • DESC: descending order • The ORDER BY clause comes last in the SELECT statement. SELECT name from Account Order by Name limit 10
  • 18. For child-to-parent relationships • Query child-to-parent relationships, which are often many-to-one. Specify these relationships directly in the SELECT, FROM, or WHERE clauses using the dot (.) operator. SELECT Contact.FirstName,Contact.Account.Name from Contact
  • 19. For parent-to-child relationships • Specify these relationships using a subquery (enclosed in parentheses), where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause • When you use a relationship name in a query, you must use the relationship names without the __c. Instead, append an __r (underscore underscore r). SELECT Name , ( SELECT Contact.FirstName,Contact.LastName FROM Contacts ) From Account
  • 20. Click to editSOQL in Apex
  • 21. Querying Record in Batches By Using SOQL For Loops • SOQL for loops iterate over all of the sObject records returned by a SOQL query • SOQL for loops can process records one at a time using a single sObject variable, or in batches of 200 sObjects at a time using an sObject list insert new Account[]{new Account(Name = 'yyy'), new Account(Name = 'yyy'), new Account(Name = 'yyy')}; Integer i = 0; for (Account tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) { i++; } System.assert(i == 3); i = 0; Integer j; for (Account[] tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) { j = tmp.size(); i++; } System.assert(j == 3); System.assert(i == 1);
  • 22. What is SOSL To perform text-based queries across multiple sObjects, you can use SOSL (Salesforce Object Search Language)
  • 23. SOSL • FIND Clause with Search Term • IN Clause :- search group • RETURNING :- which data to return • When you use a relationship name in a query, you must use the relationship names without the __c. Instead, append an __r (underscore underscore r). FIND ‘PathToCode’ IN ALL FIELDS RETURNING Account(Name), Contact(LastName, FirstName, Email)
  • 24. Q & A