What is text-to-SQL?
Text-to-SQL is the task of turning a question in plain language, such as "what was revenue last month?", into a SQL query that answers it. Today it is usually done by a large language model given the question and a description of the database schema.
How an LLM writes the query
The model never sees the database directly. It sees a prompt: the question, the SQL dialect, and some description of the schema, usually table and column names with types, sometimes keys, sample values and column descriptions. From that it predicts a query. A tool then runs the query and, in an agent setup, shows the result back to the model so it can fix errors or refine the answer.
Academic benchmarks such as Spider use small, clean schemas. Real warehouses are harder: hundreds of tables, abbreviated column names, and business rules that exist only in someone's head. Most failures come from missing context, not from SQL syntax.
Why column meanings matter
Take an orders table with columns amt_c and st. The names say little. Here is what a model writes from the names alone, and what it writes once it knows that amt_c is the order total in cents and that refunded orders don't count as revenue.
-- From the names alone SELECT sum(amt_c) AS revenue FROM orders WHERE created_on >= DATE '2026-10-01' AND created_on < DATE '2026-11-01'; -- With column meanings: amt_c is cents, st = 'refunded' is excluded SELECT sum(amt_c) / 100.0 AS revenue_usd FROM orders WHERE st = 'paid' AND created_on >= DATE '2026-10-01' AND created_on < DATE '2026-11-01';
On a 20,000-row test table, the first query returns 109,794,000 and the second 988,290.0. Both are valid SQL and both run without error. The first is off by more than 100x, and nothing in the result says so.
Common failure modes
| Failure | Example | What prevents it |
|---|---|---|
| Wrong unit | Summing cents as dollars | Units in column descriptions |
| Join fan-out | Joining orders to order items, then summing order totals | Declared keys and join cardinality |
| Invented columns or tables | WHERE country = ... on a table without a country column | Exact schema in the prompt; validating the query before running it |
| Wrong business definition | Counting refunds, test accounts or cancelled orders | Declared metrics with their filters |
| Wrong filter values | status = 'complete' when the data says 'paid' | The column's allowed values |
| Dialect mismatch | Functions from another database | Telling the model the dialect; parsing before execution |
Guardrails
- Run generated SQL with read-only credentials unless writes are the point.
- Parse or EXPLAIN the query before running it, and price it if the engine can, so a runaway scan is caught first.
- Cap the rows returned to the model.
- Show the SQL next to the answer, so a person can check the logic and not just the number.
When the model also chooses follow-up queries on its own, text-to-SQL becomes agentic analytics, and the same guardrails matter more.
In Thunderduck
Thunderduck keeps a description, a role and a unit for each column, inferred by Claude from names, types and key definitions (no data values are sent), and corrected by people in the console. Each meaning carries its provenance, inferred, confirmed or edited, so an assistant knows which ones to trust. Over MCP, get_catalog_context returns a catalog's tables with keys, declared joins and cardinality, column meanings, declared measures with their filters, and value domains that say whether a list of allowed values is complete.
Generated SQL then goes through the same guards as any client: token scopes, a price computed from the plan before the query runs, per-query and daily cost limits, and structured refusals rather than silent failures.
Docs: https://docs.thunderduck.ai/ai/semantics/