SQL: LIMIT without ORDER BY returns an arbitrary set of rows

Row order is not a property of a table. Without ORDER BY the engine returns whatever the plan produced first, and that changes with the plan, the statistics or a vacuum.

Code
SELECT * FROM orders LIMIT 10;                    -- arbitrary 10
SELECT * FROM orders ORDER BY id DESC LIMIT 10;   -- the 10 newest

-- ORDER BY must also be deterministic. If created_at has ties, add a
-- tie-break or pagination will repeat and skip rows:
SELECT * FROM orders ORDER BY created_at DESC, id DESC LIMIT 10;
Output
-- Same query, twice, on an unchanged table:
--   run 1: ids 4, 9, 12, ...
--   run 2: ids 118, 4, 9, ...   (after an autovacuum reorganised pages)
--
-- This is the single most common cause of "the same page shows different
-- results on refresh".
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25