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.
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
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:
Fields: This field exists to represent the fields to be selected.
Object: The object field is used to represent standard and custom objects.
WHERE: This field is used to specify the condition in a field.
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.
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.
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”.
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.
2. DESC
SELECT S_NAME S_PERCENTAGE FROM Student ORDER BY S_PERCENTAGE DESC
The above SOQL Query returns the following Records
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
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.
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
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Salesforce Training | Dec 24 to Jan 08 | View Details |
Salesforce Training | Dec 28 to Jan 12 | View Details |
Salesforce Training | Dec 31 to Jan 15 | View Details |
Salesforce Training | Jan 04 to Jan 19 | View Details |
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.