Skip to content

Non ancora disponibile in italiano. Vedi tutto ciò che è disponibile in italiano

Stockertim Dictionary · Artificial intelligence · Technology

Text-to-SQL

Also called text to SQL, NL2SQL, natural language to SQL

In short

Turning a question written in plain language into a database query, often with the help of an AI assistant, so the question can be run against stored data.

  • Intermediate
  • Technology
  • Checked

In detail

Text-to-SQL takes a question such as "how many orders did each region place last month" and produces a query in SQL, the standard language for asking relational databases for data. The query is then run against the database and returns the answer as a table.

To work, the tool needs to know the database structure: which tables exist, what each column means and how tables link. Given that schema, a language model writes a query that it predicts answers the question. The more precisely the schema and the question are described, the better the query tends to be.

A generated query can be wrong while still running without error. Typical faults are joining tables in a way that repeats rows, so totals double; filtering on the wrong date column; treating blanks incorrectly; or answering a slightly different question from the one asked. Reading the query before trusting the result, and checking row counts, catches most of these.

Access matters too. A text-to-SQL tool usually runs with read-only permissions on approved tables, so a misunderstood request cannot change or delete data, and so it cannot reach tables the user is not allowed to see.

Worked example

An analyst at Harbour Foods asks an assistant: "What was total order value by region last month?" The orders table has 1,000 rows for the month, worth US$64,000.

  1. The generated query joins orders to order lines and sums the order value column.
  2. The result totals US$166,400, because each order is repeated once per line: 2,600 lines for 1,000 orders.
  3. Check: US$166,400 / US$64,000 = 2.6, which equals 2,600 / 1,000 = 2.6
  4. The analyst asks for the sum to be taken from the orders table alone, and the regions now add up to US$64,000.

Reading the query and comparing with a known total caught a join that inflated the answer 2.6 times.

In practice

Business teams use text-to-SQL to answer routine questions without waiting for an analyst, and analysts use it to draft queries faster. Organisations limit it to read-only, approved data and expect people to check the query and the row counts.

Watch out

  • A query that runs without error can still answer the wrong question.
  • Joins that repeat rows are the commonest cause of doubled totals.

Learn it properly

Official sources

Facts last checked on 3 November 2026.

Education, not advice: the Stockertim Dictionary explains what terms mean. It never recommends buying or selling anything.

English