SELECT * FROM events LIMIT 10; is run repeatedly on a large table. Why can the returned rows differ between executions?
Correct answer
B. Without ORDER BY no row order is guaranteed, so the plan may return any ten rows
16 views
B. Without ORDER BY no row order is guaranteed, so the plan may return any ten rows
16 views
LIMIT without ORDER BY is non-deterministic because SQL never promises row order: a sequential scan, an index scan or a parallel plan may each return a different arbitrary subset. Adding ORDER BY on a unique key makes the result stable and reproducible. The same reasoning applies to pagination with OFFSET, where unstable ordering can repeat or skip rows.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is B? Ask here and get an explanation.