What is Text-to-Structured Query Language (Text-to-SQL)?

Text-to-SQL is a language model application that reads the database schema, turns a natural language question into an executable SQL query and presents the result as a table or explanation.

It differs from Retrieval-Augmented Generation in source type: RAG retrieves text chunks from documents to write an answer, while text-to-SQL queries structured data in database tables for precise numeric results. The query can also be designed as a tool run through the model's tool calling.

Topic
Embeddings, document processing and RAG
Also known as
NL2SQL, natural language to SQL

How Text-to-Structured Query Language (Text-to-SQL) works

  1. The model gets the question with names, columns, relations and sample values of relevant tables.
  2. The model writes an SQL query, checked for syntax and permitted tables.
  3. The query runs on a read-only account under row and time limits.
  4. The result is shown as a table or short explanation; if the query fails, the error goes back to the model for a fix.

In more detail

Schema linking in large schemas

Real enterprise databases often contain hundreds or thousands of tables, making it impossible to place the entire schema in the prompt without adding noise. Schema linking uses retrieval or lightweight classifiers to select only the relevant tables, foreign keys and columns for each question.

The semantic layer and a business glossary

Even with a clear schema, business terms such as 'active customer', 'net revenue' or 'delay' are rarely self-evident from column names alone. Pairing Text-to-SQL with a governed semantic layer or business glossary prevents conflicting SQL definitions across departments.

Evaluation: execution accuracy and SQL dialects

Text-to-SQL accuracy is typically evaluated with Execution Accuracy (EX) and Valid Efficiency Score (VES) rather than string matching. Execution Accuracy checks whether the generated query returns the same result set as the reference SQL, because different valid queries can produce identical results.

Row-level security (RLS) in the database

Enforcing tenant or department restrictions by instructing the LLM to add a WHERE clause is unsafe against prompt injection; access control must be enforced inside the database engine via Row-Level Security (RLS) or user-scoped views.

Commonly confused terms

TermHow it differs
Parameterised tool callingText-to-SQL lets the LLM write flexible ad-hoc SQL queries from schema metadata, whereas deterministic tool calling invokes pre-written, parameterised queries or APIs with fixed business logic.

Why Text-to-Structured Query Language (Text-to-SQL) matters for on-premises AI

Many managers' questions are answered by databases rather than documents, such as “which region saw returns rise last month”. Text-to-SQL opens this data to staff who do not know SQL. The risks are real: the model can write a syntactically valid but semantically wrong query, presenting a wrong number with confidence. Generated SQL is therefore shown to users, queries run read-only within the user's own data permissions and important reports are checked by an expert.

ON-PREMISES EXAMPLE

A logistics company's operations manager asks “the five warehouses with the most delays last week” in plain language; the system writes the SQL, runs it on a read-only account and shows the result with the query.

Common questions

How does text-to-SQL differ from RAG?

RAG answers from document text. Text-to-SQL writes queries against database tables, suiting precise numeric questions such as counts, totals and comparisons.

How is text-to-SQL set up safely?

Run queries on a read-only account limited to the user's data permissions, list permitted tables, show generated SQL to users and set row and time limits.

Sources

In the glossary: Glossary, R to Z

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.