Blog

SOQL Of Salesforce

  • (4.0)
  • | 1480 Ratings
SOQL Of Salesforce

SOQL(Salesforce Object Query Language) Of Salesforce:

SOQL is a query language to search your organization's data for relative information. It is similar to SQL, it uses Object query language but not Joins for immediate navigation of data. It is similar to the SELECT statement used for query language (SQL). It is used to retrieve data, data of a single sObject & its related sObjects.

How to fetch data from the DataBase?

The database is a collection of tables which stores data in it. To fetch or retrieve data from a DataBase, it is simple by using SOQL & SOSL querying in Salesforce.

SOQL - Salesforce Object Query Language.
SOSL - Salesforce Object Search Language.

The above two languages help us to deal with DB, to fetch the required data.

Apex Code = Logic.
Apex Code = logic + Data Fetching Capability.

Introduction to SOQL - Salesforce Object Query Language

  1. This helps developers to deal with DB.
  2. This deals with all types of objects present in the Salesforce DB.(standards, custom)
  3. This language works on the queries provided by it.
  4. The query is nothing but the set of keywords that we need to use properly with the help of few object names as well as conditional statements - it will bring the required output.
  5. These queries evaluate to a list of SObject
  6. This evaluates to single SObject.
  7. This also evaluates to count of those objects (using “count” method).

How to write SOQL?

A SOQL query starts with SELECT clause and followed by 1 or more optional clauses which include WHERE, GROUP BY, TYPE OF and ORDER BY. In the below-mentioned format, a SOQL query is queried.

The syntax for SOQL Query:

SELECT <fields>FROM <Object name>WHERE <Conditions>

SELECT: Every SOQL query must start with this keyword. This keyword tells us which “fields” from the XYZ Object we are interested in.

FROM: This keyword help us to mention in which “object” we are interested.

WHERE: This keyword helps us to mention the “condition” of fetching records. I.e, only those records we can fetch which match our business requirement, means condition.

Example: SELECT name FROM Levis__c WHERE Price__c > 600

Checkout SalesForce Tutorial

Where to Test These SOQL queries?

We have two ways to perform this,

1. Apex Class.
2. Anonymous Window

Anonymous Window:

Steps: Login to Salesforce → Developer Console → Press the bottom arrow of the window and select “Query Editor” Tab and place the code and “Execute”.

SOQlquerytray

SOQL Advanced Query Examples:

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE Experience__c > 4

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE (Experience__c > 4 AND Country__c = INDIA)

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE (Experience__c = 4 OR Experience__c = 1)

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c Order By Experience__c

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE Experience__c <> null Order By Experience__c

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE Experience__c <> null Order By Experience__c LIMIT 2

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE Experience__c <> null Order By Experience__c DESC

Aggregate Functions:

SELECT MAX(Experience__c) FROM Naukri_com_JobForm__c

SELECT MIN(Experience__c) FROM Naukri_com_JobForm__c

SELECT SUM(Experience__c) FROM Naukri_com_JobForm__c

SELECT AVG(Experience__c) FROM Naukri_com_JobForm__c

Note: You cannot add any fields like First Name / Last Name in the above query, Salesforce will throw an error, as other fields are not supported in aggregate.

Checkout Salesforce Interview Questions

Wild Characters / LIKE Keyword:

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE First_Name__c LIKE ‘j%’

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE First_Name__c LIKE ‘%ar%’

SELECT First_Name__c, Experience__c FROM Naukri_com_JobForm__c WHERE First_Name__c LIKE ‘jo__’

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

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

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The 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.


DMCA.com Protection Status

Close
Close