# Text-to-SQL

> Canonical: https://skillalmanac.com/glossary/text-to-sql | Verification status: pending | Index status: not offered to search engines

Text-to-SQL is the task of turning a question written in plain language into a SQL query that a database can run, often done by a language model given the database's schema.

Also called: text to SQL, NL2SQL, natural language to SQL, natural language to SQL query, AI SQL generator.

## In plain language

Text-to-SQL lets you ask a database a question the way you would ask a colleague, for example which customers ordered twice last month, and have the computer write the database query (SQL) for you. It is useful when you do not know SQL or want a first draft. The risk is that a query can look right and still answer a different question, or touch data you did not mean it to. A model that has only the table names often lacks the business meaning behind them. Read the query before you trust the answer, and give the tool read-only access.

## Technical definition

A text-to-SQL system takes a natural-language question and a description of the database, typically the schema of tables and columns, and produces a SQL string. The Spider benchmark paper (Yu et al., arXiv 1809.08887, submitted 24 September 2018) defines the task across many databases and uses different databases in training and testing, so models must generalize to schemas they have not seen. Snowflake's Cortex Analyst documentation says generic AI solutions often struggle with text-to-SQL when given only a database schema, so it adds a semantic view of business concepts, metrics and relationships. It also states that it is limited to questions that can be resolved with SQL.

## How it shows up when you use agents

When a coding agent turns your question into a database query, that step is text-to-SQL: it reads the question, looks at the schema, writes a query and runs it, often through a database [MCP server](/glossary/mcp-server) or [tool call](/glossary/tool-calling). Business-intelligence products offer the same thing behind a chat box. The [large language model](/glossary/large-language-model) writes the SQL, and the database runs it.

What breaks when it is misunderstood: people read the answer and skip the query. A query can join the wrong tables, filter on the wrong column or count duplicates, and still return a tidy table. Column names often do not carry the business meaning, which is why Snowflake adds a semantic view of metrics and relationships. A second risk is write access. A model that can run any statement can change or delete data, and text taken from a database can carry instructions aimed at the model, see [prompt injection](/glossary/prompt-injection).

Next actions: connect the agent with a read-only database account, paste the schema and a few example rows into the prompt, ask the agent to show the SQL beside the result, and check one answer by hand. The [sql-and-database-helper](/skills/skill-almanac/sql-and-database-helper) skill is built around a read-only default.

## Common confusions

- **Text-to-SQL versus RAG.** Text-to-SQL asks the database to compute the answer from structured tables. [RAG](/glossary/retrieval-augmented-generation) finds passages of text. Some systems use both.
- **Generating SQL versus running it.** Writing a query is a text task. Running it is an action with consequences. Keep the two steps separate and review before running anything that writes.
- **Benchmark versus your database.** Spider tests generalization to schemas unseen in training. Your database has its own naming, quirks and business rules, so a benchmark does not predict the accuracy you will get.
- **Myth.** Plain-language questions make SQL knowledge unnecessary. You still need enough SQL to read the query and spot a wrong join or filter.

## Worked examples in this catalogue

### [sql-and-database-helper](https://skillalmanac.com/skills/skill-almanac/sql-and-database-helper)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Safe SQL and schema changes for PostgreSQL, MySQL and SQLite: read-only by default, with a static checker.

Why it exercises the concept: It covers the part text-to-SQL leaves to you: deciding whether a generated query is safe to run. The read-only default fits the advice to limit what a generated query can do.

### [records-to-sql-inserts](https://skillalmanac.com/skills/skill-almanac/records-to-sql-inserts)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: SQL CREATE TABLE and INSERT statements generated from a CSV or JSON records file, with inferred column types and escaped text values.

Why it exercises the concept: It produces a schema and sample rows you can hand to a model as context. It does not answer questions in plain language, so it is a setup step, not the whole task.

### [code-risky-pattern-scan](https://skillalmanac.com/skills/skill-almanac/code-risky-pattern-scan)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A read-only scan of a source folder for risky code patterns, including string-built SQL.

Why it exercises the concept: Applications that build SQL strings from model output or user text are exposed to injection. The scan looks for that pattern in code you already have.

### [Optimize a SQL Query With an AI Agent: Start From the Plan](https://skillalmanac.com/learn/optimize-a-sql-query-with-an-ai-agent)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The guide asks an AI agent to improve a query starting from the EXPLAIN plan, with the schema and row counts supplied.

Why it exercises the concept: A generated query that returns the right rows can still be slow. The guide shows how to give an agent the plan, not just the query, and check the result.

## Related terms

- [Large language model (LLM)](https://skillalmanac.com/glossary/large-language-model.md)
- [Natural language processing (NLP)](https://skillalmanac.com/glossary/natural-language-processing.md)
- [Tool calling (function calling)](https://skillalmanac.com/glossary/tool-calling.md)
- [Prompt injection](https://skillalmanac.com/glossary/prompt-injection.md)
- [Guardrails](https://skillalmanac.com/glossary/guardrails.md)
- [Retrieval-augmented generation (RAG)](https://skillalmanac.com/glossary/retrieval-augmented-generation.md)

## Sources

- [Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing and Text-to-SQL Task](https://arxiv.org/abs/1809.08887), arXiv (Yu et al.) (accessed 2026-11-24)
- [Cortex Analyst](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst), Snowflake Documentation (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above. Independent source and example-target verification is pending. [Glossary index](https://skillalmanac.com/glossary.md).
