Text-to-SQL data: how many correct SQL statements per question
A Text-to-SQL score means nothing unless the metric, the split and the database snapshot are all stated; what a buyer should fix is execution-based scoring plus a cross-database split, not a single accuracy number.
On this page
You are accepting a batch of Text-to-SQL training data. The vendor's report says 500 samples, 82% accuracy. You run it yourself: exact string match gives 61. You load the SQL into the database, execute both sides and compare result tables: 84. Three numbers, one dataset, one model, one afternoon, and nobody made an arithmetic mistake. The twenty-odd point spread is three people describing three different things. This article separates those three things and says which one belongs in the acceptance form.
Start with three different accuracies
The 61, 82 and 84 above are invented, used only to show how much the metric matters; they are not numbers from any real report. In a real project the values differ, but a spread of ten-plus points between metrics is normal.
Here is how each is computed. First, exact string match: normalise whitespace, case and table aliases, then compare the predicted SQL character by character against the gold SQL. Second, clause-level partial match: split SELECT columns, WHERE predicates, GROUP BY, ORDER BY and aggregates, score each, and average — so a half-right query still earns points. Third, execution match: run both gold and prediction against the same database snapshot and compare the returned tables, usually after normalising column and row order.
Why a twenty-point spread? A semantically perfect query that happens to be written differently scores zero under the first metric and full marks under the third. Conversely, a query that copies the gold structure and gets one constant wrong in WHERE scores well under the first two and fails outright under the third. The two biases push in opposite directions, so the three numbers never resemble each other.
Spider made switching databases a hard constraint
Spider is the most cited dataset in this category. It holds a bit over ten thousand questions paired with more than five thousand human-written SQL statements, spanning two hundred databases across more than a hundred domains. The impressive part is not the scale but the split: the databases used for training and for testing do not overlap.[1]
That design closes a specific shortcut. If training and test share databases, a model can memorise table names, column names, even the answer to a particular question; the score looks strong and collapses on your own schema. A cross-database split forces the model to read the schema and interpret what a column means in the business, which is the actual task.[1]
For a buyer this means the split is part of the specification, not an internal implementation detail. The same rows split randomly versus split by database can produce very different scores, and the inflated part is exactly the capability you need: writing a query against a schema you have never seen. Asking whether test databases overlap with training databases is more informative than asking for the accuracy.
Mind the reporting metric too. Spider pairs each question with one human-written gold SQL, so string comparison marks a semantically equal but structurally different query as wrong. The published numbers are therefore exact-match numbers, not execution numbers.[1] Do not compare your execution accuracy against them directly; those are two different rulers.
Six places where two correct queries diverge
Unpack multiple correct answers and the divergence points are few — six, mostly. The first three are stylistic: the SQL means exactly the same thing. The last three are semantic: results can genuinely differ. Keeping the two groups apart tells you what execution matching removes and what it quietly lets through.
| Divergence point | Two ways to write it | String match | Execution match |
|---|---|---|---|
| Join order and aliases | Join A then B, or B then A; alias t1 versus a | Marked wrong | Passes |
| Subquery versus join | WHERE id IN (SELECT ...) versus an equivalent INNER JOIN | Marked wrong | Passes |
| De-duplication | SELECT DISTINCT versus GROUP BY every output column | Marked wrong | Passes |
| Counting | COUNT(*) versus COUNT(col); differs when col has NULLs | Marked wrong | Differs; the meaning really differs |
| Ordering and truncation | LIMIT 10 without ORDER BY: which ten rows is undefined | Marked wrong | May pass by luck |
| Aggregates over NULL | SUM skipping NULLs versus summing after COALESCE | Marked wrong | Usually identical |
The last two rows matter most. Row five shows execution matching has its own randomness: LIMIT without ORDER BY can return different rows on each run, and a model that guesses right once gets credit. Row six shows execution matching cannot separate a different style from a different meaning — it sees only the result, and an equal result is not a correct query.
So execution matching is not a looser string match; it is a different ruler with its own errors. It fixes false negatives caused by style and introduces false positives caused by coincidence. Knowing that is what stops you from treating it as the more accurate score.
An example you can recompute yourself
Invent a tiny schema with two tables, unrelated to any real dataset: employees(id, name, dept_id, salary, hired_at) and departments(id, name, budget). The question: which department, among those with a budget above one million, has the highest average salary?
-- Schema (invented)
-- employees(id, name, dept_id, salary, hired_at)
-- departments(id, name, budget)
-- Form A: GROUP BY + ORDER BY + LIMIT
SELECT d.name
FROM departments d
JOIN employees e ON e.dept_id = d.id
WHERE d.budget > 1000000
GROUP BY d.id, d.name
ORDER BY AVG(e.salary) DESC
LIMIT 1;
-- Form B: window function, same meaning as A
SELECT name FROM (
SELECT d.name, RANK() OVER (ORDER BY AVG(e.salary) DESC) AS rk
FROM departments d
JOIN employees e ON e.dept_id = d.id
WHERE d.budget > 1000000
GROUP BY d.id, d.name
) t
WHERE rk = 1;
-- Form C: looks right, means something else
SELECT d.name
FROM departments d
JOIN employees e ON e.dept_id = d.id
WHERE d.budget > 1000000
GROUP BY d.id, d.name
ORDER BY SUM(e.salary) DESC
LIMIT 1;A and B are guaranteed to agree on any dataset, and string matching scores them as two entirely different answers. C differs only in SUM versus AVG: it picks the department with the largest payroll, which is the department with the most people, not the highest average salary. In real organisations those are frequently not the same department.
The crucial part is the last step. If only one department clears the budget threshold, C returns exactly the same row as A and execution matching calls it correct. That is not a contrived case — small evaluation sets are full of questions with a single candidate. You can verify it: five rows in employees, two departments, one just over the threshold, and you will see when C gets lucky and when it fails.
Five things to fix at acceptance
- Ask for the metric first, and ask for two: string exact match and execution accuracy, with the normalisation rules in writing — case, whitespace, aliases, and whether column or row order affects the verdict.
- Ask for three attachments: a database snapshot (or a build script plus data), the schema DDL, and the gold SQL for every sample. Without a snapshot, execution scoring cannot run and you are back to string matching.
- Write twenty parallel items yourself: two tables from your own warehouse, a hand-written question and gold SQL, plus a second semantically equivalent form. Measure how often the model's equivalent form is marked wrong — that is the size of the metric penalty.
- Have a human read thirty disagreements: pull the cases that fail string matching but pass execution, and decide item by item whether it is style or a wrong query that got lucky. It cannot be automated, but thirty items show the trend.
- Put the split and the snapshot in the contract: test databases disjoint from training databases, a fixed schema version, a fixed snapshot date. If any of the three moves, earlier scores are void.
Step four is the one people skip, because it is slow and does not fit a CI pipeline. It is also the only step that separates style from luck. Most procurement arguments are not about model strength; they are about two parties who never looked at those thirty disagreements together.
Where this metric stops working
Execution matching depends on database state. The same query returns different rows after an update, and renaming a column breaks the gold SQL outright. Execution scoring must therefore be pinned to a snapshot, or it becomes less stable than string matching — strings at least do not change when the data does.
The other edge is that execution matching loosens the whole evaluation. An empty join result, a wrong column that happens to hold the same values, LIMIT 1 on a single-candidate question — all pass. Loosening a metric does not make a model worse, but it makes scores look better, and better-looking scores are never neutral in a negotiation.
One thing you can do immediately: next time a Text-to-SQL sample arrives, run both metrics yourself. If they differ by more than fifteen points, do not discuss price — ask for the thirty disagreement cases. If you cannot get them, the vendor's accuracy is an adjective, not an acceptance criterion.
