SOQL (Salesforce Object Query Language) is a query language that searches for and retrieves data from Salesforce objects. SOQL is similar to SQL, but it is specifically designed for the Salesforce platform and supports features such as relationship queries and custom object searches.
Let’s take an example here, suppose you want to get the below information about a person
Name
Age
Relationship
If you have a table where the information is stored. You make a query to get the details
Syntax of SOQL
SELECT FieldAPIName1, FieldAPIName2 FROM Object_ApiName__c(Account/Contact/Payment__c)
SELECT one or more fields
FROM an object WHERE filter statements results are ordered (optional)
Here are some key concepts and examples of SOQL in Salesforce:
WHERE
Single Filter – ( Name = ‘United Oil’)
Multiple Values Filter – Name IN (‘United Oil’, ‘Acme’)
Like Filter – Name Like ‘%United Oil’
%abc% – Keyword is found anywhere in the name
%abc – The starting char could be anything but the ending will always be abc
abc% – Starting will always be ABC and the ending will be anything
Not Like Filter
Not Name Like ‘TEST%’
CreatedDate > 2021-09-12T22:16:30.000Z
ORDER BY
ORDER BY Name DESC
ORDER BY Name ASC
LIMIT
LIMIT 200
COUNT()
COUNT()
COUNT(ID)
COUNT(AnnualRevenue)
GROUP BY – Aggregate Query
Aggregate Methods
count
sum
Integer
Number
Currency
min
Integer
Number
Currency
Date
DateTime
max
Integer
Number
Currency
Date
DateTime
GROUP BY Name
GROUP BY ROLLUP(LeadSource)
GROUP BY ROLLUP(Status, LeadSource)
GROUP BY CUBE(Type, BillingCountry)
How the Query should be formed
The Key Element or the Keywords that we have just talked about should be placed in the right place otherwise the SOQL will not work.
Below image shows at which place we need to which keyword
Watch Complete Video Here
Assignment
List All the Contact Records
List All the Contact Records where the Email is not blank
List All the Contact Records Where the Email is Blank
List All the Contact Records Where the Title contains the developer
List All the Contact Records Where the Title Starts with the Manager
List All the opportunities where the Amount is more than 29000