Data and retrieval

Text-to-SQL

Text-to-SQL is an AI technique in which a language model turns a plain-English question, such as "what were sales by region last quarter", into a SQL query, runs it against a database and returns the answer.

Most business questions about numbers are answered by someone who knows SQL writing a query. Text-to-SQL lets people ask those questions directly in plain language. A model reads the question, works out which tables and columns are involved, writes the query, runs it and explains the result. For buyers it promises faster answers and fewer requests queued for the data team, as long as the answers can be trusted.

How it works

The model needs to understand the database before it can query it. A build therefore gives it a description of the relevant tables, columns, relationships and, crucially, business definitions: what "revenue" means, which table holds active customers, how fiscal quarters are defined. This context can be written into the prompt or retrieved per question.

When a question arrives, the model drafts SQL. The system checks the query, runs it against a read-only connection, and either returns a table and chart or passes the results back to the model to summarise. If the query fails, the error is fed back and the model tries again. Many builds show the generated SQL so analysts can verify it.

An example: a sales director asks "Which ten customers grew the most year on year?". The model joins orders to customers, groups by year, calculates growth and returns the list with the query underneath.

Text-to-SQL differs from retrieval-augmented generation: RAG finds passages of text, while text-to-SQL computes exact answers from structured data.

What it means for cost

The connection and chat interface are the simple part. Cost is driven by how well the data is organised and documented. A clean data warehouse with clear table names and agreed metrics makes the build much smaller; dozens of cryptically named tables with conflicting definitions need a modelling and documentation phase first.

Safety adds work too: a read-only database user, access control so people only see data they are allowed to, guardrails against costly or runaway queries, and an evaluation set of real questions with known correct answers to measure accuracy before launch; see evals. Ask a builder how wrong answers will be caught and how the model learns the business's definitions. Live figures are on the text-to-SQL analytics cost page.

Common questions

How accurate is text-to-SQL?

Accuracy varies with the complexity of the question and the quality of the schema documentation. Simple lookups and aggregations are usually handled well; questions that depend on unstated business rules are where errors creep in. Testing on the buyer's own questions is the only reliable measure.

Is it safe to let an AI query the company database?

It can be, with the right setup: a read-only connection, limited tables, row-level permissions, query timeouts and logging. The model should never have write access, and sensitive tables should be left out of what it can see.

Related terms

Pricing a build that involves Text-to-SQL? Send the brief and get a fixed price set next to the market range.Get a quote