← All articles
Salesforce · SOQL · Quick Reads

SOQL ORDER BY, LIMIT, OFFSET, and Pagination Patterns

Sorting and limiting are easy to add to SOQL, but reliable pagination requires more thought.

ORDER BY

SELECT Id, Name, CreatedDate
FROM Account
ORDER BY CreatedDate DESC

If multiple records can share the same sort value, add a deterministic tie-breaker:

SELECT Id, Name, CreatedDate
FROM Account
ORDER BY CreatedDate DESC, Id DESC

LIMIT

SELECT Id, Name
FROM Account
ORDER BY CreatedDate DESC
LIMIT 100

LIMIT is useful for bounded UI results, diagnostics, and queries where the business requirement genuinely needs only the first N records.

OFFSET

OFFSET can skip rows for relatively small pagination scenarios:

SELECT Id, Name
FROM Account
ORDER BY Name
LIMIT 100
OFFSET 100

But OFFSET is not a general strategy for walking millions of Salesforce records. It has platform limits and becomes the wrong abstraction for large extraction jobs.

Prefer Cursor/Locator-Based API Pagination for Large Results

Salesforce query APIs can return a query locator / next-records mechanism when more records exist. Consumers should follow that server-provided continuation mechanism rather than inventing page numbers over a changing dataset.

For very large asynchronous extraction, evaluate Bulk API.

Keyset-Style Checkpoints for Integrations

For incremental integrations, a stable ordered checkpoint is often more useful than OFFSET:

SELECT Id, SystemModstamp
FROM Account
WHERE SystemModstamp >= 2026-08-23T12:00:00Z
ORDER BY SystemModstamp, Id

Persist enough checkpoint information to resume safely, and design the consumer to tolerate duplicates around boundaries.

LIMIT Without ORDER BY

If your requirement is "the newest 10" or "the highest 20," always express the ordering. LIMIT alone does not communicate which records are important.

Reference

CONTINUE READING

Explore closely related architecture, integration and implementation topics.