Hi folks, In this tutorial, You will learn what is SOQL Queries in Salesforce and Some SOQL Queries example. SOQL is an acronym for Salesforce object query language. Which can be used to retrieve data from Salesforce org’s database. The syntax for writing SOQL queries is very similar to the SQL Query.

What is the SOQL Query in Salesforce?

SOQL is an acronym for Salesforce object query language. Which can be used to retrieve data from Salesforce org’s database.
If you are familiar with SQL (Structured Query Language) then you will find SOQL Syntactically very similar to it but is customized for the lightning platform. Because Apex has direct access to your Salesforce database you can embed SOQL queries in your Apex code and get results.

The syntax for writing SOQL query:

select field1, field2, field3,... from object
Where condition
Order By fields

Example of SOQL Queries:

1- Basic SOQL Query:

SOQL query to select Account Name, Billing City, Billing State, Phone Number from Account Object.

SELECT Name, BillingCity,BillingState, Phone FROM Account

2- Where Condition:

String Comparison:

SOQL Query to select all Account with Billing City “Lawrence”.

SELECT Name, BillingState, Phone, From Account Where BillingCity='Lawrence'

Number Comparison:

SOQL query to select all Account with Number of Employees equal to 10

SELECT Name,BillingCity, BillingState, Phone FROM Account NumberOfEmployees=10

SOQL Query to select all Account with Number of Employees greater than or equal to 10;

SELECT Name,BillingCity, BillingState, Phone FROM Account NumberOfEmployees>=10

SOQL Query to select all Account with Number of Employees less than or equal to 10;

SELECT Name,BillingCity, BillingState, Phone FROM Account NumberOfEmployees<=10

Where clause with And and OR operator:

Select all accounts which have Billing City and Billing State.

SELECT Name, BillingState, Phone, From Account Where BillingCity!=null AND BillingState!=null

select all which either have Billing City and Billing State or have Billing Country.

SELECT Name, BillingState, Phone, From Account Where (BillingCity!=null AND BillingState!=null) OR (BillingCountry!=null)

Filter Records with Picklist value:

Select all accounts which Account Source equal to Web.

SELECT Name, BillingState, Phone, From Account Where AccountSource='Web'

Filter Records with ID field:

Select account record for specific Account Id.

SELECT Name, BillingState, Phone, From Account Where Id='001i000000kFxeG'

Filter Records by Date:

Select all Accounts which are created today.

SELECT Name, BillingState, Phone, From Account Where CreatedDate=today

Like Operator in Where clause:

Select all Account records with Billing City starts with ‘LW’.

SELECT Name, BillingState, Phone, From Account Where BillingCity Like 'LW%'

Select all Account records with Billing City ends with ‘CE’

SELECT Name, BillingState, Phone, From Account Where BillingCity Like '%CE'

Select all Account records with ‘RE’ in between Billing City.

SELECT Name, BillingState, Phone, From Account Where BillingCity Like '%RE%'

Order By clause:

Select all Account records Order By Name in Ascending order.

SELECT Name, BillingState, Phone, From Account Order By Name ASC

Select all Account records Order By Name in Descending order.

SELECT Name, BillingState, Phone, From Account Order By Name DESC

Limit Result:

Select Top 50 records with Account Name in Ascending Order.

SELECT Name, BillingState, Phone, From Account Order By Name ASC Limit 50

Complete Example of SOQL Query:

SELECT Name, BillingState, Phone, From Account Where (BillingCity='Lawrence' AND BillingCity!=null AND BillingState!=null) OR (BillingCountry!=null)

Order By Name ASC Limit 100

View More:

Conclusion:

I hope this is a useful topic for you if you have just started working with SOQL. Your feedback and suggestions are important for me. Please feel free to comment for any technical help.

Thank You.

Leave a Comment