Home  >  Blog  >   Salesforce

Salesforce SOQL

Rating: 4
  
 
7877
  1. Share:
Salesforce Articles

Introduction to Salesforce SOQL

The Name SOQL is Popularly Known as  Salesforce Object Query Language. Salesforce endorses some of the features of SQL. It is almost similar to SQL(Structured Query Language). With the help of SOQL, we can retrieve the data.

Characteristics of SOQL

  • SOQL Minimizes our time and Workload.

  • SOQL queries are necessary for Apex Programming.

  • We can understand SOQL easily, compared to SQL.

  • Good Knowledge in SOQL helps for becoming the right Developer.

  • SOQL assists in Code Optimization.

As it is Case insensitive, we can use both lowercase and uppercase letters in SOQL Queries. SOQL Code optimization gives fast results. Data is retrieved from Salesforce standard objects and custom objects with the support of Salesforce Object Query Language.

Want to enhance your skills in dealing with the worlds best CRM, enroll in our Salesforce Training 

SOQL Syntax

we use “Select” Keyword to retrieve data from objects and fields.

Example For SOQL Query:

SELECT ID, Salary FROM Employee

Through the above SOQL Query, we can retrieve the ID and Salary of the Custom Object called Employee.

Fields exist in a SOQL Query are as follows:

  1. Fields: This field exists to represent the fields to be selected.

  2. Object: The object field is used to represent standard and custom objects.

  3. WHERE: This field is used to specify the condition in a field.

  4. OFFSET: To represent the starting row of the record

In SOQL Queries, we use API names for fields.

In Standard Objects, Field names represent API Name.

In Custom Objects, Column Name Represents API Name.

 MindMajix YouTube Channel

How to Write SOQL QUERY

SOQL Query is confined in Square Brackets. The below SOQL Query retrieves the record from the database which has Salary Field equal to “50000.”

Employee e = [ Select ID, Name from Employee Where Salary=’50000’]

In the above SOQL Query, the variable “e” is used to store ID and Employee fields whose Salary is Equal to “50000”.

WHERE Clause in SOQL 

The “WHERE” clause is used to refine the records based on a condition.

Example:

SELECT SID FROM Student WHERE Percentage>85

In the above SOQL Query, we are using WHERE Clause to retrieve the selected data from the “Student” object. The WHERE Clause applies the “Percentage>85” condition to filter the records in the Student Object.

Comparison Operators in SOQL

In SOQL, we use comparison operators to compare two values and returns TRUE or FALSE. Various Types of SOQL operators are as follows:

1. Equals(=) operator

Through the below query, we can see the usage of Equals(=) operator.

Select S_Name, S_ID FROM Student WHERE S_Percentage=”80.”

The above SOQL Query retrieves S_Name and S_ID of the students whose Percentage is equal to 80.

2. Less than (or) equal to operator

   The following query illustrates the purpose of Less than (or) equal to the operator:

SELECT S_Name, S_ID FROM Student WHERE S_Percentage<=85

The above SOQL Query retrieves S_Name, S_ID of the students whose Percentage is less than or equal to 85

3. Greater than (or) equal to operator

The following query demonstrates the objective of Greater than (or) equal to the operator:

SELECT S_Name, S_ID FROM Student WHERE S_Percentage>=90

The above SOQL Query retrieves S_ID and S_Name of the students whose Percentage is greater than or equal to 90.

4. Lesser than operator

The purpose of Lesser than operator is demonstrated with the help of the below query:

SELECT S_ID, S_CLASS FROM Student WHERE S_FEE<10000

The above SOQL Query retrieves S_ID and S_CLASS of the students whose fee is Lesser than 10000.

5. Greater than operator

The objective of greater than operator is demonstrated with the help of the below query:

 SELECT S_Name S_CLASS FROM Student WHERE S_FEE>10000

The above SOQL Query retrieves S_Name and S_CLASS of the students whose fee is greater than 10000

6. Like

 The Like operator is used to retrieve the records which contain the required string. Like operator is illustrated by the following example:

SELECT S_NAME S_ID FROM Student WHERE S_ID LIKE ‘00%

The above SOQL Query returns all the records where S_ID contains “00”.

ORDER BY Clause

In SOQL, we will use ORDER BY Clause to retrieve the records either in ascending or descending order depending upon the given condition.

Salesforce supports two types of ORDER BY Clause, they are:

  • ASC

  • DESC

1. ASC

SELECT S_NAME S_FEE FROM Student ORDER BY S_FEE ASC

The above SOQL Query returns the following Records.

S_NAMES_FEE
Ramesh10000
Suresh15000
Rakesh25000

2. DESC

SELECT S_NAME S_PERCENTAGE FROM Student ORDER BY S_PERCENTAGE DESC

The above SOQL Query returns the following Records

S_NAMES_PERCENTAGE
Ramesh95
Suresh92
Rakesh90
Rajesh89
 

Logical Operators in SOQL

In SOQL, logical operators are used to linking one or two conditions in a single query. The logical operators that exist in SOQL are as follows:

1. AND

The Logical “AND” operator retrieves the records when all the conditions present in the SOQL Query are fulfilled. The below query demonstrates the logical “AND” operator.

SELECT S_NAME S_FEE FROM Student WHERE S_FEE>5000 AND S_FEE<10000

S_NAME

S_FEE

Ramesh

6000

Suresh

7000

Rakesh

9000
 

2. OR

In SOQL, the OR operator is used to retrieve the data if any of the conditions present in the SOQL Query is satisfied.

SELECT S_NAME S_FEE  FROM Student WHERE S_NAME =” VISWANATH” OR S_NAME=” RAKESH.”

The above query returns the following records.

S_NAME

S_FEE

Rakesh

9000

 

Conclusion

In Salesforce, SOQL acts as a query language used to retrieve the required data from the database. By following the syntax of the SOQL, salesforce professionals can develop the queries. It will have certain clauses and operators and depending upon your requirement you can use them.

In the next topic, we will discuss in detail about “SOSL Of Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration TrainingSalesforce Lightning Training
Salesforce Advanced Developer TrainingSalesforce Developer Training
Salesforce IoT TrainingSalesforce App Builder Certification Training
Salesforce AppExchange TrainingSalesforce Service Cloud Training
and many more. 
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingApr 27 to May 12View Details
Salesforce TrainingApr 30 to May 15View Details
Salesforce TrainingMay 04 to May 19View Details
Salesforce TrainingMay 07 to May 22View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read more
Recommended Courses

1 / 15