AI GLOSSARY · AI CONCEPTS

Text-to-SQL

Markdown version

Definition

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.

Worked examples in this catalogue

Catalogue examples point at a skill, guide or coding tool listed on this site, with what to run and what you see. An example marked illustrative describes the item from its own documentation; it was not executed for this page. Platform Scores are our assessment under a published method, not a safety guarantee.

  1. Skill by Skill Almanac · Illustrative, not executed

    sql-and-database-helper 57/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  2. Skill by Skill Almanac · Illustrative, not executed

    records-to-sql-inserts 42/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  3. Skill by Skill Almanac · Illustrative, not executed

    code-risky-pattern-scan 42/100 · Provisional, not yet measuredScored · Method v0.2 · How we score

    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.
  4. Guide · Illustrative, not executed

    Optimize a SQL Query With an AI Agent: Start From the Plan

    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.

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 or tool call. Business-intelligence products offer the same thing behind a chat box. The 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.

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

More in AI concepts

Next steps

  1. Optimize a SQL Query With an AI Agent: Start From the Plan The long-form guide.
  2. sql-and-database-helper A skill that exercises it.
  3. Large language model (LLM) Related term.
  4. Retrieval-augmented generation (RAG) Related term.
  5. All glossary terms A to Z and by family.

Sources

Definitions are written by Skill Almanac from these sources. An independent review of the sources and examples is in progress. Wikipedia is never the only source.

Review record

Independent review in progress

Drafted by claude-sonnet-5-5. An independent review of its sources and examples is in progress.

This entry is still in review, so it is not listed in search engines yet.

Change log (3)
  • : First version.
  • : Verifier pass: applied verifier corrections.
  • : Corrected explicit MCP-server anchor; link-only verification does not assert complete content approval.