Order is not free
SQL makes no ordering guarantee unless you explicitly use ORDER BY. A query that "happened to come back sorted" yesterday will silently change order tomorrow when the planner picks a different access path. Always use ORDER BY when order matters.
Sorting
- Ascending is the default;
ORDER BY price DESCfor descending. - Multiple columns sort lexicographically:
ORDER BY category, price DESC. NULLS FIRST/NULLS LASTcontrols where NULLs land (PostgreSQL extension).- You can sort by computed expressions or column position (
ORDER BY 2 DESC) — both are legal but expression is more readable.
Pagination — and why OFFSET stops scaling
LIMIT 10 OFFSET 100 still has to read and discard the first 100 rows. At page 1000 you're discarding 10,000 rows per request. Cursor-based (keyset) pagination is the scalable answer: filter on the last seen value, then LIMIT. No OFFSET, constant cost regardless of page depth.