← My notebook

03 · Data

Text to SQL

A question in English is not a query. Text to SQL turns it into one, runs it on real tables, and returns rows instead of a guess.

From words to a query

"Which sites lost the most fuel this week?" has not touched a table. The model's job is translation: map "sites" to a column, "lost fuel" to a sum of litres, "this week" to a filter.

That only works if the model knows the schema. Table names, column names, and what a row means are the context that matters most.

The database does the work

Once written, the query runs on the database, not in the model. WHERE keeps week 42. Week 41 is still in the table; it is skipped, not deleted. GROUP BY adds up each site.

In the picture, E14-02 is 260 plus 160, which is 420. N1-18 is 180 plus 80, which is 260. The model never did that addition. The database did.

Why rows beat prose

A number that came from a query can be traced. You can rerun it, read the SQL, and argue with the filter. A number the model wrote from memory cannot be checked at all.

What to watch for

Show the SQL next to the answer. Run read-only. Limit rows. And test the common questions against known results, because a query that runs is not the same as a query that is right.