Monday , April 22 2019
Home / Apex / Apex Programming for beginners / SOQL (Salesforce Object Query Language)

SOQL (Salesforce Object Query Language)

SOQL (Salesforce Object Query Language)

SOQL is the Salesforce Object query language, it is similar to SQL in syntax. SOQL is a tool which helps us to retrieve a record from database in Salesforce. A sample SOQL is written below:

Select Id, Name from Account;


The result of the above statement is a list of accounts, i.e. a SOQL returns a list of sObject. We can rewrite the above statement as below.


List<account> examplelist = [Select Id, Name from Account];


Individual records can be accessed from this list using a loop. We can use SOQL to create a new sObject as well.


Contact newContactObject = new contact(Account = [select name from account where billingCity = ‘new delhi’ limit1 ];)

newContactObject.firstname = ‘test’;

newContactObject.lastname = ‘contact’;


Working with the result of SOQL Query


The result of the query only include the fields which are mentioned in the query, if we try to access a field which was not fetched in the query then we end up having a runtime error.


Account a = [Select id from account limit 1].name    —- this will throw runtime error

Account a = [Select id,name from account limit 1].name    —- this will run uninterrupted


Accessing fields of sObject through relationship in SOQL


There are two types of relationship when it comes to fetching record from SOQL

  1. Fetching values of parent record from a child record
  2. Fetching all the child records of a parent.


The first scenario is achieved using a dot notation in SOQL query as written below:


Select id, name, parentobj.name, parentobj.custfield__c from myObject__c limit 1;


The second scenario is achieved by using a nested query as mentioned below:


Select id, name, (select firstname, lastname from contacts) from account;


When we write a nest query we make use of the child relationship name as ‘contacts’ is the relationship name between account and contact, we have used the plural of contacts. It is a user defined text for a custom relationship.


In the custom relation we make use of ‘__r’ instead of the regular ‘__c’ keyword, as written below.


Select Id, name, custObject__r.name  from contact limit 1;


*Note, it is advised that we should avoid searching for records which contains a null value, this helps in improving the performance of the query.


How to use Apex variable in SOQL Queries


SOQL can reference apex variable in the statement, variable has to be prefixed with a colon(:) as written below:


Contact con = new contact(lastname = “test”, firstname =”contact”);

Insert con


Select name from contact where Id =:con.Id;


We can use apex variable in the where clause, In or Not In clause, numeric values of limit or offset.


SOQL can also fetch the records which have been soft deleted from the org i.e. they are still present in recycle bin. We just have to add literal ALL ROWS at the end of the query to fetch all the records in an org, as mentioned below.


Select name from contact ALL ROWS;


How to write SOQL for a for Loop

Records returned by the SOQL can be used as an input for for loop the syntax is written below.


For( myVar : [Query]){



Variable should be of same sObject type as of the SOQL query.


Regular SOQL and SOQL in the for loop differ from each other on the basis of the method they use to fetch record from the database, SOQL for loop use efficient chuning of data and make calls to query and querymore method where as standard SOQL can only return number of records or number of sObjects.


We should make sure that we are not making any DML call inside for loop as it results in breach off governor limit. Therefore we should use a list of sObject and add the individual objects to this list and then after the execution of the loop we should perform a DML Operation.



About Sumit Datta

Sumit Datta
I am a 3x Certified Salesforce developer with overall 5 years of IT experience and 3 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Check Also

Jitterbit Cloud agent with Salesforce

How to use Jitterbit Cloud agent with Salesforce

How to use Jitterbit Cloud agent with Salesforce     Have you ever found yourself …


How to create a Modal box in Lightning Component?

How to create a Modal box in Lightning Component? In this post we are going …

Salesforce Lightning Training

How to send parameters in AuraEnabled methods in Salesforce Lightning Components?

How to send parameters in AuraEnabled methods in Salesforce Lightning Components? Passing value to and …

Salesforce Lightning Tutorials

How to use lightning accordion

How to use lightning accordion and how to dynamically add accordion section in lightning. In …


  1. Pingback: DML Operation | SalesforceNextGen

  2. Pingback: Sumitdatta | Pearltrees

  3. Pingback: Insights of SOQL and Relationship Queries on Force.com - SalesforceNextGen

  4. Pingback: Advanced Formula, Salesforce Platform unleashed!!! - SalesforceNextGen

  5. Pingback: Dreamforce Global Gathering 2017, my experience and why one should not miss this event. - SalesforceNextGen

  6. Pingback: Salesforce Lightning Tutorial: Lightning Component basics - SalesforceNextGen

  7. Pingback: Salesforce Trailhead Widget for your Website - SalesforceNextGen

  8. Pingback: Trailhead Leaderboard(Unofficial) - SalesforceNextGen

  9. Pingback: Top 50 Salesforce Lightning Interview Questions 2018 - SalesforceNextGen

  10. Pingback: How to do JSON Parsing of a wrapper class in Salesforce - SalesforceNextGen

  11. Pingback: How to create a Connected App in Salesforce? - SalesforceNextGen

  12. Pingback: How to Authenticate user in Salesforce Using REST Api? - SalesforceNextGen

  13. Pingback: - SalesforceNextGen

  14. Pingback: How to Authenticate user in Salesforce? - SalesforceNextGen

  15. Pingback: How to Authenticate user using Username-Password Authentication Flow in Salesforce? - SalesforceNextGen

  16. Pingback: Custom setting

  17. Pingback: How to use Custom setting in Salesforce? - SalesforceNextGen

  18. Pingback: How to fetch and display list of records in Lightning Component - SalesforceNextGen

  19. Pingback: How to use lightning accordion - SalesforceNextGen

  20. Pingback: How to send parameters in AuraEnabled methods in Salesforce Lightning Components? - SalesforceNextGen

  21. Pingback: How to create a Modal box in Lightning Component? - SalesforceNextGen

  22. Pingback: Writing a Simple Apex Trigger !!

  23. Pingback: Salesforce : Dynamic Apex

Leave a Reply

Your email address will not be published. Required fields are marked *