Text-to-SQL demos are easy to make impressive. You type "show me revenue by region last quarter", the model writes a neat query, and the chart appears. The hard part comes later, when real users ask real questions against a real schema and you need to know: how often is the generated SQL actually right?
You cannot answer that by trying a few prompts by hand. You need a benchmark. This post explains how to build one for your own database, what to measure, and which mistakes make Text-to-SQL numbers look better than they are.
Why public leaderboards are not enough
Public datasets such as Spider and BIRD are useful for comparing models in general. But your production accuracy depends on things those datasets do not contain:
- Your table and column names, which are often cryptic (
tbl_ord_hdr,cust_flg2). - Your business vocabulary: what "active customer", "net revenue" or "churned" means in your company.
- Your data quirks: soft-deleted rows, status codes, multiple currencies, time zones.
- Your security rules: which tables a given user may query at all.
A model that scores well on a leaderboard can still fail most of your users' questions. The only number that matters is accuracy on your questions against your schema.
Step 1: Collect real questions
Start with questions people actually ask. Good sources are support tickets, analyst request queues, saved reports and dashboard filters. Aim for 30 to 150 questions to begin with. A small, honest set beats a large, artificial one.
Make sure the set covers different difficulty levels:
- Simple lookups: "How many orders were placed yesterday?"
- Joins: "Top 10 customers by revenue with their account manager."
- Aggregations and grouping: "Average order value per month this year."
- Business terms: "How many active subscribers do we have?", where "active" has a specific definition.
- Ambiguous questions: "Show me sales", where a good system should ask for clarification or state its assumption.
- Hostile or out-of-scope requests: "Delete all test orders" or "show me employee salaries", which must be refused.
Step 2: Write reference answers, not reference SQL
A common mistake is to store one "correct" SQL query per question and compare the model's SQL text with it. Two queries can look completely different and return the same result, or look almost identical and return different results.
Instead, for each question store the expected result: the rows and values a correct query returns on a fixed snapshot of the database. Then judge the model by execution accuracy: run its SQL and compare the result set with the reference.
Practical tips:
- Freeze a test copy of the database so expected results do not change over time.
- Compare results order-insensitively unless the question asks for an order ("top 10").
- Allow small numeric tolerance for floating-point values.
- Ignore column aliases; compare values.
Step 3: Classify every result
"Accuracy" as a single number hides the information you need. Record each run in one of these buckets:
| Outcome | Meaning |
|---|---|
| Correct | Query ran and returned the expected result |
| Silently wrong | Query ran but returned a wrong result |
| Failed | Invalid SQL, error at execution, or timeout |
| Unsafe | Query attempted something forbidden (write, DDL, restricted table) |
| Refused correctly | System declined an out-of-scope or hostile request |
Silently wrong is the most dangerous bucket. A failed query is annoying, but the user knows something went wrong. A silently wrong query returns a believable number, and someone may make a decision based on it. Track it separately and try to drive it toward zero, even at the cost of more refusals.
Step 4: Establish a baseline
Run the benchmark with the simplest possible setup first: the question plus a raw schema dump, sent to the model. This shows how much each later improvement actually helps.
As a real example, in a 32-question benchmark against a SQL Server sample database, a local 7B coding model (qwen2.5-coder:7b running in Ollama) started at 17 of 32 correct, or 53%. Of the rest, 7 were silently wrong, 8 failed and 2 were unsafe. That baseline looked nothing like the demo.
Step 5: Change one thing at a time
With a baseline in place, improve the pipeline step by step and re-run the benchmark after every change:
- Better schema context. Describe tables and columns in plain language, include foreign keys and a few example values. In the example above, a schema-aware prompt moved the same model from 17 to 26 of 32 (81%).
- Validation and repair. Parse the generated SQL before running it, reject anything that is not a single SELECT, and send errors back to the model for one repair attempt. That took the example to 28 of 32 (88%), and the unsafe queries never reached the database.
- Business knowledge. Add a glossary of business terms with definitions and synonyms, retrieved per question.
- Model choice. Only after fixing context and validation, compare models. Many teams switch to a bigger model when the real problem was missing context.
The step-by-step build behind these numbers is documented in AI Database Agent, Part 1: a first Text-to-SQL baseline and Part 3: validated SQL.
Step 6: Automate it
A benchmark you run once is a report. A benchmark you run on every change is a safety net. Build a small console app or test project that:
- Loads the question set and expected results.
- Calls your real Text-to-SQL pipeline, not a simplified copy.
- Executes generated SQL against the frozen test database with a read-only user and a timeout.
- Writes a summary (correct, silently wrong, failed, unsafe) and a per-question diff.
- Fails the build if accuracy drops or any unsafe query appears.
Run it whenever you change the prompt, the model, the schema description or the validation rules. Prompt changes that "obviously improve" things regularly break questions that used to work.
A simple report format
Keep the output of every benchmark run in the same shape so you can compare runs over weeks and months. A useful report has three parts:
- A summary line with the date, model, prompt version and the five outcome counts, for example correct 28, silently wrong 2, failed 2, unsafe 0, refused correctly 3.
- A per-category table showing accuracy for simple lookups, joins, aggregations and business-term questions, so you can see where the pipeline is weak.
- A diff list of questions whose outcome changed since the previous run, with the old and new SQL side by side.
The diff list is the part people read. When a prompt change fixes four questions but breaks two others, you see both immediately and can decide whether the trade is worth it. Store the reports in your repository next to the code, so every pull request that touches the AI pipeline shows its effect on accuracy.
Mistakes that inflate Text-to-SQL accuracy
- Testing on questions you used to write the prompt. Keep a held-out set the prompt author has not seen.
- Counting "query ran" as success. Execution without error says nothing about correctness.
- Judging by eye. A human glance at SQL misses wrong joins and missing filters. Compare results.
- Ignoring safety. A pipeline that answers 95% of questions but occasionally emits a DELETE is not production-ready.
- One run only. Models are not perfectly deterministic. Run each question several times at your production temperature and report consistency.
What good looks like
There is no universal target, but a reasonable bar for internal analytics tools is:
- High accuracy on simple and join questions.
- Silently wrong answers close to zero, with uncertain cases refused or flagged.
- Zero unsafe queries reaching the database, enforced by code rather than by the prompt.
- Every answer showing the SQL it ran, so users can verify.
If you are building the pipeline itself, How to Build Text-to-SQL with ASP.NET Core covers the architecture: schema context, generation, parsing, read-only execution and result formatting.
FAQ
How many questions do I need?
Start with 30. That is enough to spot big differences between approaches. Grow toward 100 to 200 as you see real usage.
Should I use an LLM to judge correctness?
Use execution-based comparison first. An LLM judge can help with free-text explanations, but for SQL results, comparing rows is cheaper and more reliable.
Do local models work for Text-to-SQL?
Yes, for many internal use cases. With good schema context and validation, a 7B coding model can be useful, and it keeps your data on your own hardware. Measure it on your own benchmark before deciding.
Conclusion
Text-to-SQL quality is not a feeling, it is a number you can measure. Collect real questions, store expected results, classify every outcome, and re-run the benchmark on every change. Most of all, watch the silently wrong bucket, because that is where trust is lost.
More practical guides on Text-to-SQL, RAG and AI agents in .NET are on AI Engineering Foundry.