What is SOQL?
SOQL Stands for Salesforce Object Query Language.
Use: - To Query the data from the Salesforce object.
Where Fields/columns are written in between SELECT and FORM and Object is
written after FORM Keyword
SELECT and FORM are two main keywords in SOQL.
Eg.
[SELECT Id, Name, Phone FROM Account ]
In SOQL we need to give the API name of Fields and sobjects.
WHERE keyword is used to give conditions in SOQL.
Eg.
[SELECT Id, Name, Phone FROM Account WHERE Rating=’Hot’]
When we want to add multiple conditions in SOQL AND & OR keywords are used.
Eg.
[SELECT Id, Name, Phone FROM Account WHERE Rating=’Hot’ AND
Industry=’Transportation’]
[SELECT Id, Name, Phone FROM Account WHERE Rating=’Hot’ OR
Industry=’Transportation’]
Now if we want to give multiple conditions to the same field then we need to use
OR Keyword many times.
Eg.
[SELECT Id, Name, Phone FROM Account WHERE Industry=’Transportation’ OR
Industry=’Consulting’ OR Industry=’Energy’]
Apparao puranwad| SFDC
It's not a good practice in such situation we can use IN Keyword
When we want to use multiple field values in one field then the IN keyword is
used.
Eg.
[SELECT Id, Name, Phone FROM Account WHERE Industry IN (’Transportation’,
’Consulting’, ’Energy’)]
LIKE Keyword is used when we want to get record by letter name
[SELECT Id, Name, Phone FROM Account WHERE Name LIKE ‘G%’]
ORDER BY keyword is used to sort the records in ascending and descending.
For Ascending ASC is used and for Descending DESC is used
Eg.
[SELECT Id, Name, Phone FROM Account ORDER BY Name ASC]
[SELECT Id, Name, Phone FROM Account ORDER BY Name DESC]
Date format and date literals
To get the record created on Today
[SELECT Id, Name, Phone, CreatedDate FROM Account WHERE CreatedDate =
Today]
LIMIT keyword
When we want to retrieve first any number of records use the LIMIT keyword.
Eg.
[ SELECT Id, Name, Phone FROM Account WHERE Rating=’Hot’ LIMIT 3]
Apparao puranwad| SFDC
When we want to skip first any number of records then use the OFFSET keyword.
Eg.
[SELECT Id, Name, Phone FROM Account WHERE Rating=’Hot’ OFFSET 5]
Relationship Queries
When we want to write a Query with more than one object but there should
share relation in between that object.
Parent and child relationship
When we want to query a child record from a parent object in this case the main
object will be the parent object.
Eg.
[SELECT Id, Name, Phone, Industry, (SELECT Name, Phone FROM Contacts) FROM
Account]
When we are writing parent to child query for custom objects then child custom
object name should be written with suffix __r ( underscore underscore r)
Eg.
[SELECT Name, Phone__c, (SELECT Name__c, Book__c, FROM Students__r) FROM
School__c]
Child to parent relationship
When we want to query a parent record from a child object in this case the main
object will be the Child object.
Eg. For Standard objects.
[SELECT Id, Name, Phone, Email, Account.Rating, Account.Industry FROM contact]
Eg. For Custom objects.
[SELECT Id, Name, School__c, Class__c, Fees__c, School__r.Name, School__r.City
FROM Student__c]
Apparao puranwad| SFDC
Aggregate function in Soql
[SELECT Nameofaggrigatefunction(Name of field) FROM Account]
Eg.
[SELECT COUNT(Id) FROM Account]
[SELECT MAX(AnnualRevenue) FROM Account]
Thankyou
Apparao puranwad| SFDC