Back to Databases FAQs
HELP CENTER · FAQDatabases1 min read

Why should database queries use a stable sort order?

A query without an explicit order does not promise a consistent row sequence. Add an ORDER BY that reflects the intended view, and include a unique tie-breaker when multiple rows can share the sort value.

Databases

Make the desired order explicit

The database is free to return matching rows in an order that depends on its plan or storage layout. The same query may appear in a different order after writes, index changes, or maintenance.

For pagination, sort by a stable field such as creation time and then by a unique key. Sorting by a non-unique value alone can move tied rows between pages, creating omissions or duplicates as records change.

Keep pagination boundaries consistent

Use the same ordering and filter for every page. If the dataset can change while pages are being read, consider a cursor or snapshot approach supported by your database rather than relying on a moving offset alone.

Test with rows that share sort values and with insertions between page requests. Confirm that the resulting sequence is deterministic enough for the application and that any query limits are applied after the intended ordering.

Back to Databases FAQs