# 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.

Also called: natural language to SQL, NL2SQL, chat with your database, conversational analytics, AI data analyst. Category: Data and retrieval. Source: https://aibuildindex.com/glossary/text-to-sql

## In the index

- Chat with your data: median $2,500, middle half $500 to $6,400, 439 priced briefs (https://aibuildindex.com/cost/text-to-sql-analytics)
- SQL: median $4,800, middle half $1,000 to $14,220, 460 priced briefs (https://aibuildindex.com/skills/sql)
- Business intelligence: median $4,900, middle half $1,300 to $10,800, 112 priced briefs (https://aibuildindex.com/skills/business-intelligence)

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](https://aibuildindex.com/glossary/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](https://aibuildindex.com/glossary/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](https://aibuildindex.com/glossary/access-control) so people only see data they are allowed to, [guardrails](https://aibuildindex.com/glossary/guardrails) against costly or runaway queries, and an evaluation set of real questions with known correct answers to measure accuracy before launch; see [evals](https://aibuildindex.com/glossary/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](https://aibuildindex.com/cost/text-to-sql-analytics).

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

- [Data warehouse](https://aibuildindex.com/glossary/data-warehouse): A data warehouse is a central database designed for analysis, where cleaned data from many business systems is stored together so teams can run reports, dashboards and AI queries across all of it.
- [Large language model (LLM)](https://aibuildindex.com/glossary/large-language-model): A large language model is an AI model trained on very large amounts of text to predict the next word, which lets it write, summarise, translate, classify and answer questions in natural language.
- [Guardrails](https://aibuildindex.com/glossary/guardrails): Guardrails are the checks and controls placed around an AI model, such as input filters, output validation, topic limits and action restrictions, that keep its behaviour within safe, permitted and useful bounds in a live system.
- [Access control](https://aibuildindex.com/glossary/access-control): Access control is the set of rules and mechanisms that decide who or what can see, change or act on which data and functions in a system, so that users, staff and AI agents only reach what their role requires.
- [Retrieval-augmented generation (RAG)](https://aibuildindex.com/glossary/retrieval-augmented-generation): Retrieval-augmented generation is a way of answering questions with a language model that first looks up relevant passages in your own documents, then writes an answer grounded in what it found.
- [Evals (AI evaluation)](https://aibuildindex.com/glossary/evals): Evals are structured tests that measure how well an AI system performs on a fixed set of realistic inputs, scoring its outputs so that changes to prompts, models or data can be judged by evidence rather than impression.
