Sorting & limiting
Concept  ·  SQL & Querying

LIMIT without ORDER BY returns an arbitrary set — 'it worked yesterday' until the plan changes.

LIMIT caps how many rows come back; it never says which rows. Without an ORDER BY the database returns whatever the scan reached first, so an 'unordered LIMIT' is an arbitrary set that changes when the plan changes.

See it break
parcels
idgrams
1900
2300
31500
4600
51200
The trap
SELECT
  id
FROM
  parcels
LIMIT
  3;
The fix
SELECT
  id
FROM
  parcels
ORDER BY
  grams DESC
LIMIT
  3;

The 'three heaviest parcels' shortlist is whatever the scan returned — reload the table and it silently changes.

The rule

Add the ORDER BY the question implies, then LIMIT — the two together name a deterministic top-N.

Shows up elsewhere
Top-N per group
Try one →Prove it in practice →