Intermediate 15 min read

What Is SOQL?

By the end of this lesson, you'll be able to:

  • Write a basic SOQL query with SELECT, WHERE, ORDER BY, and LIMIT
  • Recognize inline SOQL syntax inside Apex

Prerequisites: String and Collection Methods

Inline SOQL

Apex lets you embed a SOQL query directly in your code using square brackets — this is called an inline query. It returns a List of the sObject type you selected:

List<Account> accounts = [
    SELECT Id, Name, Industry
    FROM Account
    WHERE Industry = 'Technology'
    ORDER BY Name ASC
    LIMIT 10
];

WHERE filters records, ORDER BY sorts them, and LIMIT caps how many come back — all familiar if you've used SQL before, with some Salesforce-specific extensions like relationship queries.

A filtered, sorted, limited query

SELECT Id, Name, Industry
FROM Account
WHERE Industry = 'Technology'
ORDER BY Name ASC
LIMIT 10

Exercise

Write a SOQL query that returns the Id and Name of every Contact whose LastName is 'Smith'.

Show hint

SELECT Id, Name FROM Contact WHERE LastName = 'Smith'

SOQL

SOQL Basics — Quick Check

1. Which clause limits the number of records a SOQL query returns?

2. SOQL can traverse relationships using dot notation, such as selecting Account.Name from a Contact query.

3. What do square brackets [ ] indicate in Apex?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

SOQL (Salesforce Object Query Language) is how Apex reads data. Inline queries are written directly in square brackets inside your Apex code and return a List of sObjects.