Why Your AI Gets a Different Answer to the Same Database Question
Asked the same question twice and got two different lists of 'top 10 customers' or 'recent signups' — with the same data, same filters, no error. Usually a LIMIT with no ORDER BY, which Postgres and MySQL don't guarantee stays consistent.
You ask "show me the 10 most recent signups." You ask again five minutes later, same words, same data — and the list is different. Not wrong, exactly, just... not the same 10 rows, with no error and no obvious reason why.
This usually isn't the AI being flaky. It's a missing ORDER BY on a query that has a LIMIT, and it's one of the more counterintuitive things about SQL for anyone who hasn't hit it before.
Tables are sets, not lists
A relational database table doesn't have an inherent order. Conceptually it's a set of rows, and unless a query explicitly asks for an order (ORDER BY), the database is free to return rows in whatever sequence is convenient for it at that moment — often tied to how the query planner decided to execute things, or the physical order data happens to sit in on disk.
SELECT * FROM signups LIMIT 10 without an ORDER BY says "give me any 10 rows," not "give me the 10 most recent." If the AI wrote that query intending "most recent" but forgot the sort, the database isn't malfunctioning when it hands back a different 10 rows next time — it's doing exactly what was asked, which just wasn't quite what was meant.
Why it feels newly broken even though nothing changed
Small, mostly-static tables often happen to return rows in something like insertion order, simply because the query planner has no reason to do anything fancier. That coincidental stability creates a false sense that the query is sorted, right up until:
- The table grows past some size where the planner picks a different execution strategy
- Rows get updated, deleted, or the table gets vacuumed/reorganized
- The exact same query just gets planned slightly differently on a given run
None of those require any change to your prompt, your AI client, or your gateway. The query was always missing its ORDER BY — the table was just quiet enough, for a while, that it didn't matter.
The fix
Anywhere a question implies an order — "most recent," "top 10," "highest value," "first 5" — the query needs an explicit ORDER BY matching that intent, and the LIMIT should come after it:
SELECT * FROM signups
ORDER BY created_at DESC
LIMIT 10
If you're not writing the SQL yourself, the useful move is naming the intent clearly in the question — "the 10 most recently created signups, newest first" — which gives the AI an explicit ordering to translate into ORDER BY, rather than leaving "recent" as something it has to infer belongs in the query at all.
The tiebreaker case that still bites you
Even with an ORDER BY, ties can reintroduce the same problem on a smaller scale. If ten rows share the exact same created_at timestamp (bulk imports and seed data do this constantly), the order among those ten is still unspecified, and can still shift between runs. Add a secondary sort key that's guaranteed unique — typically the primary key — to fully pin it down:
ORDER BY created_at DESC, id DESC
LIMIT 10
How to tell this apart from an actually-wrong answer
This is specifically about which rows you get shifting, with the right number of rows and no error — different from a JOIN inflating the total count, and different from a business-definition trap that gives a consistently wrong number every time. If two runs of "top 10" return 10 different-but-plausible rows rather than a wrong count or an error, missing ORDER BY is the first thing to check.
Read-only MCP gateway: mcpserver.design.
Related reading: