SOQL Fundamentals

Share

What Is SOQL?

SOQL (Salesforce Object Query Language) is used to retrieve data from Salesforce objects (like Account, Contact).

Simple Explanation

SOQL is Salesforce’s version of SELECT queries, designed for CRM data.


Basic Query Structure

SELECT FieldName FROM ObjectName

Real-Life Example

You want a list of customer names from Salesforce.

Code Example

SELECT Name FROM Account

This fetches all Account names.


Using SOQL in Apex

List<Account> accounts = [SELECT Name FROM Account];

Gist (Quick Revision)

SOQL is used to read Salesforce data using a SELECT-style syntax.


Filtering & Ordering Data

Filtering Records (WHERE Clause)

What Is Filtering?

Filtering allows you to fetch only the records you need.

Real-Life Example

Show only active customers, not all customers.


Code Example

SELECT Name FROM Account WHERE Industry = 'IT'

Multiple Conditions

SELECT Name FROM Account 
WHERE Industry = 'IT' AND AnnualRevenue > 100000

Ordering Results (ORDER BY)

Why Ordering Matters

It helps display data in a meaningful sequence.


Code Example

SELECT Name FROM Account ORDER BY Name ASC

Descending order:

SELECT Name FROM Account ORDER BY CreatedDate DESC

Gist (Quick Revision)

  • WHERE filters data

  • ORDER BY sorts results


Relationship Queries

What Are Relationship Queries?

Relationship queries allow you to query related records in one SOQL statement.

Simple Explanation

Fetch parent and child data together.


Parent-to-Child Query

Real-Life Example

Get an Account with all its Contacts.

SELECT Name, (SELECT LastName FROM Contacts) 
FROM Account

Child-to-Parent Query

Real-Life Example

Get Contact info along with its Account name.

SELECT LastName, Account.Name FROM Contact

Why Relationship Queries Are Important

  • Fewer queries

  • Better performance

  • Avoid governor limit issues


Gist (Quick Revision)

Relationship queries let you access related data efficiently using a single SOQL query.

  • January 5, 2026