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.
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.
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.
Subscribe to our youtube channel to get new updates..!
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
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:
SELECT S_NAME S_FEE FROM Student ORDER BY S_FEE ASC
The above SOQL Query returns the following Records.
SELECT S_NAME S_PERCENTAGE FROM Student ORDER BY S_PERCENTAGE DESC
The above SOQL Query returns the following Records
Logical Operators in SOQL
In SOQL, logical operators are used to linking one or two conditions in a single query. The logical operators exist in SOQL are as follows:
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
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.
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.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts