Latest Post:

What is text-to-SQL?

Definition

Text-to-SQL translates a question written in everyday language into an SQL (structured query language) query. A system uses information about the available tables, columns and relationships to generate the query, often with a language model. The SQL can then be reviewed and executed against the intended source.

Showing the generated query lets an analyst check its joins, filters and calculations before relying on the result. Table descriptions, relationship information and agreed business definitions help the system choose the intended interpretation.

Why it matters

Generated SQL gives an analyst something concrete to inspect. Instead of accepting a chart on appearance alone, you can see the selected tables, join conditions, filters and calculations. That is particularly helpful when a question contains an ambiguous phrase such as “active customer” or “monthly revenue.”

A query can be valid SQL and still answer the wrong business question. Before reusing it, check that it handles the intended time period, excludes the right records and combines tables without multiplying rows. Comparing a small result with a known example is often more useful than repeatedly rewriting the prompt. Save the reviewed version so future reports can use the same logic.

Example: turning a question into a reviewable query

Suppose orders contains the following rows, with order totals in euros:

order_id country status order_total
101 NL completed 100.00
102 NL completed 50.00
103 DE completed 80.00
104 FR cancelled 500.00

The question is: “Which countries have more than €100 in completed order value?” A matching query is:

SELECT country, SUM(order_total) AS completed_order_value
FROM orders
WHERE status = 'completed'
GROUP BY country
HAVING SUM(order_total) > 100
ORDER BY country;
country completed_order_value
NL 150.00

The status filter excludes the cancelled French order. Grouping combines the two Dutch orders, and HAVING applies the threshold to each country’s total. Those choices are visible in the SQL, so a reviewer can check that it answers the question. If the person meant a particular reporting period, that needs another explicit condition.

Text-to-SQL vs natural-language querying

Text-to-SQL is the translation step from a question to SQL. Natural-language querying describes the wider experience of asking questions in everyday language and receiving answers.

A conversational tool may include text-to-SQL, result checking, charts and follow-up questions. Conversely, a SQL generator can stop at producing query text. Seeing the generated SQL is useful, but does not by itself establish that the business interpretation is correct.