TRANSMISSION // 2611.29009v2
Why AI-written SQL can quietly count the same thing twice
Source published
A preprint finds that joins can make text-to-SQL systems overcount, and that describing what each measure represents helps reduce—but not eliminate—these errors.

A database query can run successfully and still give a convincing wrong answer. Joins may repeat rows, causing totals or counts to include the same underlying item more than once. A new preprint tests how often this affects benchmarks and whether information about a database’s structure helps language models avoid the mistake.
Imagine asking an AI assistant for the total shipping fees on orders that contained a toy. The fee is recorded once per order, but the query joins orders to individual order lines to find the toy orders. If a qualifying order has two toy lines, its fee appears twice in the joined rows and may be added twice. The SQL runs, and the result can look reasonable, even though it answers the question incorrectly.
This is called a fan-out error. The central issue, according to Ahmed M. Mahran’s preprint, is grain: what one value represents. A shipping fee might be recorded once per order, while a quantity is recorded once per order line. To aggregate correctly after a join, a query needs to account for both the measure’s grain and how many times the join repeats each relevant row. Table and column names alone may not reveal either fact.
Auditing answers as well as AI systems
The study asks how often benchmark “gold” answers contain fan-out errors, and whether information derived from the data can help language models avoid them.
The author built an audit that parses SQL queries and checks the rows read by aggregates, using the database itself to detect repetitions. The audit was applied to BIRD, a widely used text-to-SQL benchmark, and seven other benchmarks that could be tested in SQLite. The author also compared flagged BIRD answers with a later, independently prepared revision of the benchmark.
For model testing, eight language models acted as agents: they could inspect and query the database before submitting SQL. Their results were compared under different conditions, including access to basic column statistics, ordinary functional-dependency information, and grain-aware information. The latter describes what each measure is recorded per and how many rows may occur for each join key. The experiments included a synthetic set of questions designed to expose grain mistakes and a reviewed set of BIRD questions. In total, the study reports 38,232 agent runs.
What the study reports
On BIRD’s June 2024 development set, the audit marked 5.3% of gold queries with a definite fan-out flag. After language-model judges assessed the flagged cases, 4.2% of the gold answers were judged distorted by fan-out. These are different measures: the audit detects repeated rows, while the 4.2% figure reflects the subsequent judgment that the answer was distorted. In BIRD’s later revision, answers with definite flags were changed more often than unflagged answers: 44% versus 16%. That comparison supports the audit as a warning signal, but does not show that fan-out caused every revision.
On BIRD training questions, grain-aware information reduced the study’s measured fan-out errors from 18.4% of runs without evidence to about 13%. The improvement also appeared on held-out questions and with scoring measures that did not rely on an AI judge. However, the models still made errors, and overall answer accuracy improved less—about three percentage points compared with no evidence. Many remaining mistakes were not fan-out errors. Some benchmark hints also recommended the formula that produced the overcount.
Results differed across the enterprise data examined. The study reports distorted answers for 24% of warehouse questions drawn from query logs, about 9.1% of generated warehouse questions, and 10.3% of generated questions for a network database. These figures describe specific BEAVER datasets and question types, not enterprise databases in general.
Why the results need care
A flagged query is not automatically wrong. Sometimes a question genuinely asks for a count of rows or transactions, including repeated entities. The audit detects repetition but cannot decide what a question intended on its own. The benchmark review and some error labels also relied on language-model judges; agreement between judges does not rule out shared biases.
The evaluation has further limits. GrainTraps is small and synthetic, and its questions were not held out while the grain-aware rules were developed. The BIRD held-out questions offer a separate check, but were not a random sample. The study focused mainly on SQLite, tested a limited selection of models, and sometimes showed agents only tables already used by reference queries—a simpler setup than many real deployments. The paper also notes that some joins to subqueries are not fully covered by the audit.
The practical lesson is not that one metadata technique makes AI-generated SQL reliable. Knowing a column’s name is not the same as knowing what its values represent. Giving models information about measure grain and join repetition can reduce one class of silent errors; auditing the benchmark answers they learn from matters too. This article summarizes an arXiv preprint, which may not have undergone peer review. The author reports releasing the audit, datasets and code, but the findings should be read in light of the study’s design and limitations.
- text-to-SQL
- large language models
- database joins
- fan-out errors
- data grain
- BIRD benchmark
Reviewed cross-references
These connections were reviewed by a model, not independently fact-checked by a human. Similarity does not imply agreement; consult the original papers and the evidence below.
When a Correct SQL Answer Hides a Bad Query
Supporting evidence: Both papers identify limits of execution-based evaluation or reward, but for distinct error types: the fan-out study says BIRD’s set-based execution accuracy ignores duplicate result rows and can hide multiplicity errors, while Nair et al. show execution-only reward gives no pressure to remove output-preserving redundant joins. Together they support the bounded point that matching execution outputs does not establish query quality; they do not show that fan-out errors are output-preserving or measure the same errors.
Model review: · openai-codex/gpt-6-luna
Evidence and provenance
Selected excerpts, not complete papers. PDF extraction can omit figures or damage equations, tables and reading order; sources may be preprints.
2611.29009v2 — Original paper
Excerpt reference:
static/papers/2611.29009v2.md:89-189
Basis: Paper text
Source file SHA-256:b7a1929792a0a38ca94e6c04763a7a57693e59c88bc3fed736ca63f5e4f04f0b2611.29930v1 — Original paper
Excerpt reference:
static/papers/2611.29930v1.md:104-196
Basis: Paper text
Source file SHA-256:e0fb54f8c2a305b9f70c5ff4851aef1c06ad32b0761c073515e8a3508019c24a2611.29930v1 — Original paper
Excerpt reference:
static/papers/2611.29930v1.md:848-901
Basis: Paper text
Source file SHA-256:e0fb54f8c2a305b9f70c5ff4851aef1c06ad32b0761c073515e8a3508019c24a
Related reading
Automatically matched by shared topic keywords. These links do not imply that the sources agree or establish a consensus.
When a Correct SQL Answer Hides a Bad Query
Source published
Shared keywords: text to sql
When Should a Text-to-SQL System Abstain?
Source published
Shared keywords: large language models, text to sql
Helping AI Keep More Than One Database Interpretation in Play
Source published
Shared keywords: large language models, text to sql
Why AI Struggles to Query Official Statistics
Source published
Shared keywords: large language models, text to sql
When an AI Queries a Database, How Does It Know What You Mean?
Source published
Shared keywords: large language models, text to sql