What is SOQL in Salesforce?
Updated · By the Clientell AI team · How we write
SOQL (Salesforce Object Query Language) is the query language that reads records from Salesforce objects. You name the fields you want, the object they live on and an optional WHERE filter. It looks like SQL SELECT but follows the Salesforce data model, and it only reads data. It cannot insert, update or delete.
Key facts
- SOQL full form
- Salesforce Object Query Language
- What it does
- Reads records from one object and the objects related to it. It cannot insert, update or delete.
- Where you run it
- Apex, Developer Console, Workbench, REST and SOAP API, Salesforce CLI, Data Loader
- Queries per Apex transaction
- 100 synchronous, 200 asynchronous (Apex Developer Guide)
- Rows per Apex transaction
- 50,000 retrieved by SOQL (Apex Developer Guide)
- Build one without typing
- The free SOQL Generator writes the query for you
Clientell AI, the AI Salesforce admin, helps with SOQL in a sandbox: it writes and tests the code, and shows it to you before anything deploys. See Apex and query generation
Try it in Clientell AI
“Write a SOQL example for my Account object, test it in a sandbox, and show me the code before anything deploys.”
Clientell AI reads your org first. Platform deployments include rollback. How security works.
What does SOQL mean?
SOQL means Salesforce Object Query Language. A query reads data from a Salesforce object (like a table, for example Account or Contact) and returns records. Salesforce describes it as the language for querying your org's data, next to SOSL for searching it (SOQL and SOSL Reference).
Two things differ from SQL. First, SOQL has no JOIN keyword. You follow relationships with dot notation or a nested query. Second, SOQL only reads. To create, change or delete records you use DML (data manipulation language) in Apex, the API, or a tool like Data Loader.
SOQL query example: SELECT, WHERE, ORDER BY
Every query needs a SELECT list and a FROM object. The other clauses are optional and must follow this order: WHERE, WITH, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET (SOQL SELECT syntax).
This query returns the ten biggest open opportunities. WHERE filters the rows, ORDER BY sorts them and LIMIT caps the result. The output is one row per Opportunity with five columns.
- Text values go in single quotes: WHERE Industry = 'Banking'.
- LIKE with % is a wildcard: WHERE Name LIKE 'Acme%'.
- IN takes a list: WHERE StageName IN ('Prospecting', 'Qualification').
- Date literals replace fixed dates: CloseDate = THIS_QUARTER or CreatedDate = LAST_N_DAYS:30.
- In Apex, a colon marks a bind variable: WHERE AccountId = :accId.
SELECT Id, Name, Amount, CloseDate, StageName
FROM Opportunity
WHERE IsClosed = false AND Amount > 50000
ORDER BY Amount DESC
LIMIT 10How do you query related records in SOQL?
There is no JOIN, so you query across objects through relationships. A child-to-parent query uses dot notation on the lookup field. You can go up to five levels, for example Contact.Account.Owner.Name (Salesforce Developers). A parent-to-child query uses a nested SELECT on the child relationship name, which is usually the plural object name.
The second query below returns each Account with its open Cases in one call. Each parent-child subquery counts as an extra query against the governor limit (Apex Developer Guide).
-- child to parent: Contact, with its Account and the Account owner
SELECT FirstName, LastName, Account.Name, Account.Owner.Name
FROM Contact
WHERE Account.Industry = 'Healthcare'
-- parent to child: Account, with its open Cases
SELECT Name, (SELECT CaseNumber, Subject FROM Cases WHERE IsClosed = false)
FROM Account
WHERE Id IN (SELECT AccountId FROM Case WHERE Priority = 'High')How do aggregate queries work in SOQL?
Aggregate functions roll many records into one row. The language supports COUNT, COUNT_DISTINCT, SUM, AVG, MIN and MAX (Aggregate Functions). GROUP BY splits the result by a field. HAVING filters the groups after they are built, while WHERE filters the rows before grouping.
The query below returns one row per Opportunity.StageName with the deal count and the total Amount. Only stages above the HAVING threshold appear.
SELECT StageName, COUNT(Id) deals, SUM(Amount) total
FROM Opportunity
WHERE CloseDate = THIS_YEAR
GROUP BY StageName
HAVING SUM(Amount) > 100000
ORDER BY SUM(Amount) DESCSOQL vs SOSL vs SQL
SOQL fits when you know which object holds the data and want to filter by field values. SOSL fits when you have some text and do not know which object or field holds it. SQL belongs outside Salesforce, in a database or a warehouse.
| SOQL | SOSL | SQL | |
|---|---|---|---|
| Purpose | Query records from one object and its relations | Text search across many objects | Query any relational database |
| Many objects in one call | Only through relationships | Yes | Yes, with JOIN |
| Can write data | No | No | Yes (INSERT, UPDATE, DELETE) |
| JOIN keyword | No, uses relationship paths | No | Yes |
| Returns | A list of records | A list of lists, one per object | Rows |
| Apex limit per transaction | 100 sync, 200 async (Apex Developer Guide) | 20 searches (Apex Developer Guide) | Not applicable |
What are the SOQL governor limits?
Salesforce runs many customers on shared servers, so each Apex transaction has hard caps. Cross one and the transaction fails with a limit exception you cannot catch. See governor limits for the full set. These two matter most for queries (Apex Developer Guide).
What are common SOQL mistakes?
Most limit errors come from the same few patterns. To check a fix, run the query in the Developer Console Query Editor and compare the row count. Each has a simple fix.
Security matters too. In API version 67.0 and later, Apex runs in user mode by default and WITH SECURITY_ENFORCED is not allowed in SOQL. Use WITH USER_MODE instead (Apex Developer Guide).
- A query inside a for loop. A trigger on 200 records then runs 200 queries and breaks the limit (Apex Developer Guide). Fix: query once before the loop with WHERE Id IN :ids and keep the results in a Map.
- Selecting every field. Fix: list only the fields you need.
- A filter that cannot use an index on a large object, which can fail as a non-selective query. Fix: filter on Id, a lookup, an external ID or an indexed custom field, and check the Query Plan tool in Developer Console.
- Expecting a query to write data. Fix: run DML, or Data Loader for bulk loads.
- Handling huge data sets in one transaction. Fix: move the work to Batch Apex, which gets fresh limits for each batch.
// Respects the running user's object and field permissions
List<Account> accts = [
SELECT Id, Name, Owner.Name
FROM Account
WHERE Industry = 'Banking'
WITH USER_MODE
];FAQ: SOQL in Salesforce
What is the full form of SOQL in Salesforce?
SOQL stands for Salesforce Object Query Language. It is the language that reads records from Salesforce objects.
What is a SOQL query?
A SOQL query is a statement like SELECT Id, Name FROM Account WHERE Industry = 'Banking'. It returns matching records. You can build one with the free SOQL Generator.
What is the difference between SOQL and SOSL?
SOQL queries records from one object and its relations using field filters. SOSL searches text across many objects at once. Use SOSL when you do not know which object holds the value.
Is SOQL the same as SQL?
No. It looks like SQL SELECT, but SOQL only reads, has no JOIN keyword and works on Salesforce objects instead of tables. Relationships use dot notation and nested queries.
How many SOQL queries can you run in Apex?
100 per synchronous transaction and 200 per asynchronous one. A transaction can retrieve at most 50,000 records with SOQL (Apex Developer Guide).
Where can I run a SOQL query?
In the Developer Console Query Editor, Workbench, Salesforce CLI (sf data query), Apex, the REST API or Data Loader.
Sources: SOQL and SOSL Reference: SELECT Syntax, Apex Developer Guide: Execution Governors and Limits, Apex Developer Guide: Security and Sharing, SOQL Relationship Query Limitations, SOQL Aggregate Functions. Last reviewed .
Related Salesforce Terms
4 termsSOSL
DevelopmentSOSL (Salesforce Object Search Language) is a text-search language that looks across several objects in one call and returns a list of lists. Apex allows 20 SOSL queries per transaction, separate from the SOQL limit.
Apex
DevelopmentApex is Salesforce's proprietary, strongly-typed programming language used to execute custom business logic on the Lightning Platform.
Governor Limits
DevelopmentGovernor Limits are runtime caps enforced by the Salesforce platform to ensure no single tenant monopolizes shared resources in the multi-tenant architecture.
Data Loader
DataData Loader is a Salesforce desktop application for bulk importing, exporting, updating, deleting, and upserting large volumes of records.
What should I read next?
More in Development
View all →Apex
Apex is Salesforce's proprietary, strongly-typed programming language used to execute custom business logic on the Lightning Platform.
Visualforce
Visualforce is a tag-based markup language that lets developers build custom user interfaces hosted natively on the Salesforce platform.
Lightning Web Components (LWC)
Lightning Web Components is Salesforce's modern UI framework built on native web standards like Web Components, Shadow DOM, and ES modules.
SOSL
SOSL (Salesforce Object Search Language) is a text-search language that looks across several objects in one call and returns a list of lists. Apex allows 20 SOSL queries per transaction, separate from the SOQL limit.
Try this on your own org
Start the 14-day free trial, no card. Clientell AI asks before it changes anything in production.
14-day free trial · No credit card required