← AI glossary

AI glossary · Basic · Data engineering · Language models

Text-to-SQL

Text-to-SQL is a technology that converts a question asked in everyday language into SQL, the query language databases understand. It lets you ask questions of data directly without knowing SQL.

About this term
Level
Basic · Technical terms you often meet in AI news
Added
2026-10-09
In plain words It is similar to asking a librarian at a foreign library, in your own language, for "the most borrowed book last year," and having an interpreter turn it into search terms the library's lending system understands. But just as an interpreter who misunderstands the library's classification system would bring back the wrong book, misunderstanding the data structure leads to wrong answers.

How it works

When a user asks something like "Show me sales by region last month," the system first gathers the schema, information describing which tables and columns the database has. Feeding this schema together with the question into an LLM lets the model write an appropriate SQL statement. The generated SQL is run against the database, and the results are returned to the user as a table, chart or summary sentence. A common approach is that if an error occurs during execution, the error message is shown to the model again so it can fix the SQL.

A question becomes SQL and comes back as an answerNatural-language question → LLM; Schema info → LLM; LLM → SQL statement; SQL statement → Database(Execute); Database → Result; Database → LLM(On error)Natural-languagequestionSales by region lastmonthSchema infoTable & columndescriptionsLLMWrite SQLSQL statementDatabaseRead-only executionResultTable, chart, summaryExecuteOn error
A question becomes SQL and comes back as an answer From left to right, the question is converted into SQL and executed. If an execution error occurs, it goes back to the model along the dotted line to fix the SQL.

Why it matters

Most company data lives in databases or data warehouses, but few people know how to write SQL. So business staff had to request and wait for an analyst even to see a single number. Text-to-SQL shortens this wait and lets more people check data themselves. As LLMs have improved, it is quickly being built into internal data analysis tools and BI services.

Good to know

SQL can be grammatically correct but wrong in meaning, so even plausible-looking results may be wrong numbers. Terms like "revenue" or "active users," whose definitions differ from company to company, are especially easy for the model to handle by guesswork, so a semantic layer that predefines such terms is often used alongside it. A basic rule is to connect with read-only permissions only, so SQL that accidentally deletes or changes data cannot run. For numbers used in important decisions, it is safer to have a step where a person checks the generated SQL.

Pre-deployment checklist

Safety

  • Is it connected with read-only permissions only?
  • Is SQL that changes data blocked?

Accuracy

  • Are business term definitions documented?
  • Is the generated SQL shown to users?
  • Are important numbers reviewed by a person?
Items to check before connecting Text-to-SQL to business work.

Example

When a sales representative types "Compare the number of new customers this quarter with last quarter" into a data chatbot connected to the company messenger, the chatbot internally writes SQL, runs it on the data warehouse, and replies with the two numbers and a simple bar chart. Many BI tools and cloud data platforms offer this kind of "ask in natural language" feature, and usually show the generated SQL alongside so users can review it.

Related terms