Sub topic 5
Writing SOQL queries for export
This chapter assumes you know Salesforce objects, records, and fields, and that you can run commands in a terminal. SOQL (Salesforce Object Query Language) is the query language every export tool in this tutorial relies on. Data Loader, the Salesforce CLI, and Bulk API 2.0 all take a SOQL string and return the matching rows.
Select explicit fields
SOQL has no SELECT *. You list each field you want. For a backup this is useful, because the query documents exactly what you saved.
SELECT Id, Name, Industry, OwnerId, CreatedDate, LastModifiedDate
FROM Account
WHERE LastModifiedDate >= 2026-01-01T00:00:00Z
ORDER BY LastModifiedDateAlways include Id. Without it you cannot match backed-up rows to related records later. Date and time values are written without quotes, in UTC format.
The WHERE clause on LastModifiedDate is the basis for incremental exports. After a full export, you can later export only the records changed since the last run. Ordering by LastModifiedDate makes it easy to record where you stopped.
Read parent fields and child records
Relationship queries reach into related objects in two directions.
SELECT Id, FirstName, LastName, Account.Name,
(SELECT Id, Subject FROM Tasks)
FROM Contact
LIMIT 100Account.Namereads a field from the parent record. It uses dot notation and returns a single value per row.(SELECT Id, Subject FROM Tasks)is a subquery. It returns the child records for each contact as a nested list.
Count before you export
A count query tells you how many rows to expect.
SELECT COUNT() FROM AccountKeep this number. Later you will compare it with the number of rows in the exported CSV to check that nothing was lost.
Choose the right query for each tool
| Need | Query shape |
|---|---|
| Full copy of one object | All needed fields, no `WHERE` |
| Incremental copy | `WHERE LastModifiedDate >= <last run>` |
| Parent data in the same row | Dot notation, such as `Account.Name` |
| Child data | Separate query on the child object |
| Row count for checking | `SELECT COUNT() FROM <Object>` |
Common errors
- Missing
Id. The export works, but you cannot rebuild relationships. - Subquery in a CSV export. The tool fails or ignores the nested data. Use one query per object.
- Quoted dates.
LastModifiedDate >= '2026-01-01'fails, because date and datetime values are literals and must not be quoted. - Fields you cannot read. A query fails if your profile lacks field-level access. Run exports as a user with access to every field you need.
Exercise
You need to back up all Contacts and their Tasks to CSV files with Data Loader. Why can't you use the subquery (SELECT Id, Subject FROM Tasks) in the Contact query, and what should you do instead?
Show answer
Data Loader writes flat CSV rows and cannot export nested subquery results. Run a separate query on Task, such as SELECT Id, Subject, WhoId FROM Task, and use WhoId to match each task to its Contact Id.