Apex SOQL (Salesforce Object Query Language)

Apex SOQL (Salesforce Object Query Language)

Introduction

SOQL (Salesforce Object Query Language) is like SQL, but tailored for Salesforce data. It is used to retrieve records from Salesforce objects, such as Account, Contact, Lead, or even custom objects like Student__c. 

SOQL is used inside Apex code, anonymous windows, and developer console when you want to filter, sort, or extract data from your Salesforce org. 

 

Basic SOQL Syntax: 

SELECT field1, field2 FROM ObjectName WHERE condition

Example:

SELECT Name, Industry FROM Account WHERE Industry = ‘Technology’

Key SOQL Clauses:

Clause Purpose
SELECT Choose which fields you want to retrieve
FROM Specify the object you want to query from
WHERE Add conditions to filter results
ORDER BY Sort results (ASC/DESC)
LIMIT Limit number of records returned
LIKE Search using wildcards % or _

Real-Life Analogy

Think of SOQL like searching a contact list:

When you type a name in your phone’s contact search, you’re doing something similar to: 

SELECT Phone FROM Contacts WHERE Name = ‘Adnan’

It’s like asking Salesforce to look through its records and give you just the info you need. 

Code Example

1: Simple Query

List<Account> accList = [SELECT Id, Name FROM Account WHERE Name = ‘Edge Communications’];

System.debug(accList);

Code Explanation:

Queries all Account records where Name = ‘Edge Communications’.
The result is stored in a List<Account>.

Output:

(Id=001XXXXXXXXXXXX, Name=Edge Communications)

2: Query with LIMIT and ORDER BY

List<Account> accList = [SELECT Id, Name FROM Account WHERE Name = ‘Edge Communications’];

System.debug(accList);

Code Explanation:

Retrieves the latest 3 contacts created in Salesforce.
Uses ORDER BY CreatedDate DESC and LIMIT 3.

Output:

John Doe
Alice Smith
Mark Taylor

3: Using SOQL with Custom Object

List<Student__c> students = [SELECT Name, Grade__c FROM Student__c WHERE Grade__c >= 80];

for(Student__c s : students) {
System.debug(s.Name + ‘ scored ‘ + s.Grade__c);
}

Code Explanation:

• Queries custom object Student__c.
• Filters students who scored 80 or more in Grade__c (custom field).

Output:

Adnan scored 85
Sara scored 90

Tasks For Practice

Task 1: Write a SOQL query to retrieve all leads with the status "Open - Not Contacted".

Task 2: Get the first 5 opportunities with amount greater than 10,000, sorted by amount in descending order.

Task 3: Fetch all contacts whose first name starts with 'A'.

Task 4: Query the custom object Book__c where Author__c = 'J.K. Rowling' and print book names.

Task 5: Write a query to fetch Contact records where the LastName is 'Khan'. Print the FirstName and Email of those contacts.

Task 6: Query all Opportunity records and sort them by CloseDate descending. Print Opportunity Name and CloseDate.

Task 7: Write a query to retrieve only the first 3 Lead records created, sorted by CreatedDate. Print the Name and Status.

Task 8: Write a SOQL query to retrieve the Name and Industry fields from the Account object. Loop through the result and print each record’s name and industry.