Flowstate Technologies LLP
Data intelligence8 min read

How to improve text-to-SQL accuracy on real business data

The model is rarely the problem. Text-to-SQL accuracy is mostly decided by how well the data is modelled and how the system handles questions it should not answer.

Text-to-SQL — letting someone type a question in plain language and having an LLM write the SQL query that answers it — demos well. On a clean sample database with descriptive column names it looks finished in an afternoon. On a real business schema, with forty tables, abbreviations nobody documented and three ways to calculate revenue, accuracy falls sharply. Improving text-to-SQL accuracy is mostly not about the model.

What the text-to-SQL accuracy numbers actually say

Two figures bracket the problem. Teradata, citing dbt Labs benchmark results, reports 64.5% accuracy on an unmodelled baseline, 72.7% with a semantic layer, 90.0% on fully modelled data using Claude Sonnet 4.6, and 98.2% when the fully modelled data also has a semantic layer. At the other end, Activant's research argues that execution accuracy on real enterprise schemas can fall as low as 16.7%.

Sources: Teradata: What is conversational analytics?; Activant Capital: Conversational analytics

Both can be true. The gap between them is the gap between a schema designed for questions and one that grew organically over ten years. A wrong answer at 65% accuracy is not a minor inconvenience either: it is one confident, plausible, wrong number in three, delivered to someone who has no way of telling which one.

Where the accuracy comes from
ConditionReported accuracySource
Unmodelled data, no semantic layer64.5%dbt Labs benchmark via Teradata
Unmodelled data, with semantic layer72.7%dbt Labs benchmark via Teradata
Fully modelled data90.0%dbt Labs benchmark via Teradata
Fully modelled data, with semantic layer98.2%dbt Labs benchmark via Teradata
Real enterprise schemas (lower bound)As low as 16.7%Activant

Why text-to-SQL with an LLM fails on business data

  • Ambiguous metrics. 'Revenue' could be gross, net of returns, net of tax, by order date or by invoice date. The model picks one, and it may pick differently next time.
  • Undocumented joins. If orders join to customers through a mapping table that only one engineer understands, the model will find a shorter, wrong path.
  • Cryptic naming. Columns called amt2 or flg_x give the model nothing to reason with.
  • Permissions. A raw query can read rows the person asking should not see, unless access is enforced below the model rather than requested in a prompt.
  • Questions with no answer in the data. Asked something the database cannot answer, a model will often produce a query anyway.

Cube's argument is that raw text-to-SQL forces the model to re-derive joins, metric logic and permissions on every prompt, which is why the same question can return different numbers.

Sources: Cube: Best conversational analytics software

Seven ways to improve text-to-SQL accuracy, in order of impact

1. Model the data before you prompt the model

Build clean, analysis-ready tables: one row per order, one row per rider-week, consistent keys, readable names. This is ordinary data engineering, and the benchmark above suggests it is the largest single lever.

2. Put a semantic layer between the model and the tables

Instead of asking the model to write arbitrary SQL, let it choose from defined metrics and dimensions. The model's job becomes picking 'net revenue by hub, last week' — a far smaller, more checkable decision than composing joins.

3. Enforce access below the model

Row-level security in the database or the semantic layer, scoped to the person asking. A system prompt saying 'only show this user their hub' is not an access control.

4. Build a golden question set from real users

Collect fifty to two hundred real questions your users ask, with the correct answer worked out by someone who knows the data. Score every prompt, model or schema change against it. Without this set you cannot tell whether a change improved anything.

5. Let the system ask a clarifying question

When a question is ambiguous — 'sales' with no date range, a hub name that matches two hubs — asking is better than guessing. A text-to-SQL agent that can say 'do you mean gross or net?' is more accurate than one that cannot.

6. Show the working

Display the metric definition, filters and date range used alongside every answer. A supervisor who sees 'net collections, Hub 3, 18–24 Nov, excluding paused riders' can catch a misreading in a second.

7. Refuse when the data cannot answer

Detect questions outside the modelled scope and say so. 'I don't have spare-parts data' is a correct answer; an invented query is not.

Text-to-SQL agent or semantic layer queries?

An agent that writes free-form SQL, runs it, inspects errors and retries is flexible, and useful for analysts exploring data. For managers asking recurring operational questions, constraining the model to semantic-layer queries is usually more reliable and much easier to audit. Many systems use both: governed metrics for the common questions, a supervised agent for the long tail, with its SQL shown for review.

Build or buy

Databricks' own guidance lists what a custom build involves: an LLM provider, a text-to-SQL layer, schema introspection, access controls and a feedback pipeline. If your data already lives in Looker, Power BI or a warehouse with a native assistant, try that first — it may be enough. Building makes sense when the data spans several systems, when the questions are specific to your operation, or when you are embedding the feature in your own product for your customers.

Sources: Databricks: Embedding a chat-with-data feature in a SaaS app

When not to do this

If people ask the same ten questions every week, a good dashboard answers them faster and more reliably than any chat interface. Natural-language querying earns its place when the questions vary and the people asking cannot write SQL themselves.

Sources and further reading