Glossary

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.

DuckDB
-- 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

FailureExampleWhat prevents it
Wrong unitSumming cents as dollarsUnits in column descriptions
Join fan-outJoining orders to order items, then summing order totalsDeclared keys and join cardinality
Invented columns or tablesWHERE country = ... on a table without a country columnExact schema in the prompt; validating the query before running it
Wrong business definitionCounting refunds, test accounts or cancelled ordersDeclared metrics with their filters
Wrong filter valuesstatus = 'complete' when the data says 'paid'The column's allowed values
Dialect mismatchFunctions from another databaseTelling 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/

Related terms

Query the data where it lives.

Databases, warehouses and buckets in one SQL statement, from SQL clients, BI tools and AI agents.