Sub topic 10
Querying standard and custom objects with SOQL
This chapter assumes you know what objects, fields and records are, and that you can read API names such as Invoice__c and Account__r. It also assumes you know basic SQL. SOQL (Salesforce Object Query Language) looks like SQL but has its own rules.
The working model
SOQL reads records from one object at a time. It has no JOIN keyword. Instead, you follow relationships that you already defined on the data model. A query uses API names, not labels, so a field labeled "Invoice Amount" is written Amount__c.
A query has this shape:
SELECTlists fields.FROMnames one object.WHEREfilters rows.ORDER BYandLIMITsort and cap the result.
Standard and custom objects use the same syntax. Only the names differ.
Querying a standard object
This query reads Account and brings back each account's Contacts in the same result.
SELECT Id, Name, Industry,
(SELECT FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
LIMIT 10The part in parentheses is a child subquery. Contacts is the child relationship name, which is the plural name Salesforce gives the Contact-to-Account relationship. It is not the object name. Each returned Account holds a nested list of its Contacts.
Querying a custom object
This query reads Invoice__c and also reads a field from the parent Account.
SELECT Name, Amount__c, Status__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = 'Open'The lookup field Account__c holds the parent's Id. To reach fields on the parent record, switch the suffix from __c to __r and add a dot. So Account__r.Name is the Name of the related Account.
Two directions of traversal
- Child to parent uses dot notation. It works in
SELECTand inWHERE. For example,Account__r.Name. You can go up to five levels. - Parent to child uses a subquery in parentheses, with the child relationship name. For example,
(SELECT Name FROM Invoices__r).
For a custom relationship, the child relationship name ends in __r. You set it when you create the lookup or master-detail field. The name Invoices__r is an example, so check yours in the field's detail page.
Filtering and aggregating
The WHERE clause supports =, !=, <, >, IN, LIKE and logical operators. Text literals use single quotes. Dates use unquoted literals such as 2026-01-31 or keywords such as LAST_N_DAYS:30.
This query totals open invoices per account:
SELECT Account__c, SUM(Amount__c) total
FROM Invoice__c
WHERE Status__c = 'Open'
GROUP BY Account__c
HAVING SUM(Amount__c) > 5000
ORDER BY SUM(Amount__c) DESCHAVING filters after grouping, while WHERE filters before it.
Common errors
| Error message or symptom | Likely cause | Fix |
|---|---|---|
| `No such column 'Amount'` | Used the label, or left off `__c` | Use the API name, `Amount__c` |
| `Didn't understand relationship 'Invoices'` | Wrong child relationship name | Check the field's relationship name in Object Manager |
| Field returns `null` unexpectedly | No field-level security, or parent is empty | Check permissions, and test for a blank lookup |
| `SELECT *` fails | SOQL has no wildcard | List the fields you need |
| `Too many query rows: 50001` | An Apex transaction read too many records | Add filters, or use a `LIMIT` |
Things to watch
In Apex, use bind variables with a colon:
String status = 'Open';
List<Invoice__c> invoices = [
SELECT Name, Amount__c, Account__r.Name
FROM Invoice__c
WHERE Status__c = :status
WITH USER_MODE
];
System.debug(invoices.size() + ' open invoices');Exercise
Exercise
You need the Name and Industry of each Account that has an Invoice_c, plus the invoice Name, where Status_c is 'Open'. Which object do you query from, and how do you read the Account fields?
Show answer
Query FROM Invoice_c, because it holds the lookup. Read the parent fields with dot notation: SELECT Name, Accountr.Name, Accountr.Industry FROM Invoicec WHERE Status_c = 'Open'.
Exercise
Why does SELECT Id, Name, Invoices FROM Account fail when you want each account's invoices?
Show answer
Child records need a subquery in parentheses that uses the child relationship name and the child's fields, such as (SELECT Name FROM Invoices__r). A relationship name cannot be listed as a plain field.