Text-to-SQL Accuracy: Why Your AI Agent Gets Data Wrong
Why text-to-SQL agents return wrong numbers in 2026, exactly where accuracy breaks down, and how a semantic layer fixes it for production AI products.
By Babul Jack
Ask an AI agent for last month's revenue and it answers in two seconds, with a number that looks completely reasonable. The question that matters is whether that number matches what your finance team would report, and whether asking again tomorrow gives the same answer. Text-to-SQL accuracy is the single biggest reason AI data agents fail in production, and in client projects I've traced almost every "the AI gave us the wrong number" complaint back to the same handful of root causes.
Why text-to-SQL accuracy collapses on real business data
Text-to-SQL works by handing a large language model your database schema plus a question, and letting it write a SQL query from scratch. On clean academic benchmarks this looks nearly solved — the best systems clear 91% execution accuracy on Spider 1.0. On real enterprise warehouses, which is what every client project actually looks like, it falls apart. The , built from production-style databases that often exceed 1,000 columns, saw its best agent solve only 21.3% of tasks, and a raw GPT-4o run land at roughly 10% execution accuracy according to .
That gap isn't a model-quality problem. It's a context problem. The LLM sees column names like amount, amount_net, and tbl_ord_v3_final. It has no way to know which one your finance team means by "revenue," or that "active customer" means 30 days in one dashboard and 90 in another. So it picks the most statistically plausible interpretation and writes SQL that runs cleanly, returns a number, and answers a subtly different question than the one you asked.
What actually breaks when an AI agent writes SQL
A 2026 error analysis of 4,602 incorrect SQL queries, cited by Omni Analytics, found that schema-level errors — wrong column selection and semantic misinterpretation — accounted for 81.2% of failures, against only 18.8% for syntax errors. In other words, the SQL is almost always valid. It's just wrong.
PromptQL's breakdown of the same Spider 2.0 failure data gives a useful map of where a wrong number actually enters the pipeline:
Ambiguous question — "how's revenue?" leaves the metric, period, and segment for the model to guess.
Wrong or inconsistent definition — the same metric name computed two different ways across teams.
Wrong schema linking — 27.6% of errors, split between wrong columns (16.6%) and wrong tables (10.1%), in databases averaging 755+ columns.
Wrong rows — join fan-outs and missing filters account for 8.3% of errors; BigQuery schemas without explicit foreign keys make this worse.
Wrong calculation logic — the largest single category at 35.5%, covering bad query planning, dialect misuse, and incorrect aggregation.
No real calculation at all — the model estimates a number from text instead of running code against the data.
Non-determinism compounds all of this. The same question can produce different SQL on different runs, so the same dashboard shows a different "monthly active users" figure depending on when it was refreshed. Salesforce's own production text-to-SQL agent needed to generate 10 candidate queries per prompt and vote on consistency just to lift efficacy from roughly 50% to 80%, as reported in Omni's write-up.
The fix: how a semantic layer raises AI data accuracy
The fix that actually holds up in production is not a bigger model — it's a semantic layer sitting between the question and the warehouse. A semantic layer stores governed definitions once: what "net revenue" means, which tables join on which keys, and which edge cases a calculation has to handle. The LLM stops writing raw SQL and instead selects from metrics a human has already approved; a query engine compiles the SQL deterministically.
The accuracy gains are large and consistent across independent benchmarks:
The AtScale sweep is the one I find most convincing, because it controlled for the obvious objection — "just give the model better documentation." The no-layer arm received the full schema, plain-English column descriptions, and a knowledge base containing every formula the questions depended on. It still scored about half as well as the semantic-layer arm. Switching LLMs moved accuracy by up to 17.6 points; adding the semantic layer added 20 to 31 points to every single model tested, including the one already in the lead.
Why "just document the schema better" isn't enough
Documentation answers what a column is named. A semantic layer answers what the business means by it, and more importantly, it stops the model from ever generating the join logic from scratch. One task in the AtScale benchmark went from 21 lines of model-written SQL — four chained subqueries, six joins, nested JSON extraction, and a manual divide-by-zero guard — down to three lines once the join paths and formula were pre-defined. Both versions got the right answer; only the semantic-layer version had query budget left over to answer a follow-up question correctly too.
How to build this into a real product
If you're adding natural-language analytics to your own SaaS product or internal dashboards, here's the sequence I follow on client builds:
Model your core metrics first, not your whole warehouse. Start with the 10-20 numbers that actually go into dashboards, invoices, or board decks — revenue, churn, active users — and write down the exact formula, time window, and exclusion rules for each.
Define joins once, centrally. Every join path a metric needs should live in the semantic model, not be re-derived by the LLM on every request.
Force the agent through the layer. A semantic model handed to the LLM as reference documentation doesn't help much — Omni's 2026 benchmark found a coding agent given the full semantic model as context still wrote its own SQL and trailed the enforced-layer agent by 21 points. The LLM has to select from governed fields, not just read about them.
Let it fail loudly. A good semantic layer declines a question it can't express instead of inventing a plausible wrong number. That's a feature, not a bug — dbt Labs put it plainly: "Text-to-SQL will cheerfully give you a wrong number."
Keep an escape hatch for exploration. One-off ad-hoc questions on small, well-named tables are still fine for raw text-to-SQL; reserve the semantic layer for anything that lands on a dashboard, invoice, or customer-facing screen.
Log the plan, not just the answer. Show which metric, filters, and joins the agent selected before running the query, so a reviewer can catch a wrong field selection before it reaches a board deck.
This is exactly the kind of architecture decision that determines whether an AI feature survives contact with real users. If you're scoping a natural-language analytics feature or an internal data agent for your product, this is the core design problem my AI agent development work for clients starts with — getting the data layer right before the model ever gets a prompt.
A minimal metric definition, in practice
A semantic layer definition doesn't need to be complicated to pay off immediately. Here's the shape of a governed metric, independent of which vendor's syntax you use:
metrics:
net_revenue:
description: "Recognized revenue, excluding refunds and test accounts"
expression: "SUM(amount_net) WHERE is_test_account = false"
time_dimension: recognized_date
active_customers:
description: "Customers with at least one paid action in the last 30 days"
expression: "COUNT(DISTINCT customer_id) WHERE last_paid_action_at >= CURRENT_DATE - 30"
Once that definition exists in one place, every agent, dashboard, and report that asks for "net revenue" or "active customers" gets the same number, computed the same way, every time.
Do you need a semantic layer or just better prompts?
If your schema is small, well-named, and only a handful of people query it ad hoc, better prompts and column descriptions can get you a long way — the benchmarks above show text-to-SQL accuracy climbing as modeling improves even without a full semantic layer.
The calculus changes the moment a number leaves a chat window and lands somewhere consequential: a customer-facing dashboard, an automated report, an API response another system depends on. At that point the question isn't "can the LLM usually get this right," it's "can I guarantee this number is the same every time, for every user, with permissions enforced." That's a semantic layer's job, not a prompt's.
Why does my AI agent give different answers to the same question?
This is non-determinism: text-to-SQL can write different SQL for the same question across runs, especially as schemas evolve. A semantic layer eliminates this by compiling the same metric selection to the same SQL every time, instead of letting the LLM regenerate query logic on each request.
Is a bigger or newer LLM the fix for text-to-SQL accuracy?
No. In the AtScale BIRD-Interact sweep, switching between seven different LLMs moved accuracy by at most 17.6 points, while adding a semantic layer added 20 to 31 points to every model tested, including the best-performing one. The bottleneck is context, not model quality.
Does a semantic layer slow down AI analytics?
It usually speeds answers up. When the model already has the right tables and definitions, it doesn't burn time or tokens exploring wrong joins or retrying failed query plans, which several vendors report as a secondary benefit alongside the accuracy gains.
Can I use text-to-SQL and a semantic layer together?
Yes, and most production setups do. Route anything that lands on a dashboard, invoice, or customer-facing surface through governed metrics, and keep raw text-to-SQL available for one-off exploratory questions on well-documented tables. This same "fail loudly instead of silently" principle is also why I recommend treating AI agents as adversarial inputs for security — an agent that can't express a query should refuse, not guess.
What's the fastest way to test if this is a problem for my product?
Ask your AI agent the same business question five times in a row across a day. If the SQL or the number changes, or if two team members get different "active customer" counts from the same assistant, you have a semantic-layer problem, not a model problem. This is the same class of silent failure covered in why AI agents fail in production — it rarely announces itself with an error.
Key takeaways
Text-to-SQL accuracy collapses on real enterprise schemas: Spider 2.0 shows the best agents solving roughly a fifth of enterprise-style tasks, versus over 90% on clean academic benchmarks.
81.2% of incorrect SQL queries in a 2026 error analysis were schema-level errors — wrong columns, wrong tables, or misread business meaning — not syntax mistakes.
A semantic layer that forces the LLM to select governed metrics instead of writing raw SQL adds 20-31 accuracy points across every model tested in the AtScale BIRD-Interact benchmark.
The biggest production risk isn't a wrong answer that errors out — it's a plausible wrong number that looks right and reaches a dashboard or board deck unchecked.
Model your 10-20 core metrics and join paths first, force agents through that layer for anything customer-facing, and keep raw text-to-SQL only for low-stakes exploration in 2026.