Blog / Data and AI

Text-to-SQL explained: asking your data questions in plain English

By ideasbuilt.io. . 3 min read.

The short answer

Text-to-SQL uses a large language model to turn a plain-English question, such as “what were sales by region last quarter?”, into a SQL query, runs it against your database and returns the answer, usually as a table or chart. It works well when the model is given a clean data model, clear business definitions and read-only access, and when the generated SQL is shown so people can check it.

Key points

  • Text-to-SQL turns questions in plain English into SQL that runs against your data.
  • Accuracy depends more on the data model and definitions than on the AI model.
  • Always show the SQL. People trust answers they can inspect.
  • Run it on read-only roles with the same access rules as the rest of your data.
  • Start with a well-modelled set of tables, not the whole warehouse.

What text-to-SQL is

Most company data lives in databases that answer questions written in SQL. Most people who have questions do not write SQL, so they queue up behind an analyst. Text-to-SQL removes the queue: someone types a question the way they would ask a colleague, and an AI model writes the query.

How it works, step by step

  1. The question arrives in plain English.
  2. The system gathers context: which tables and columns exist, how they join, and what business terms like “active customer” or “revenue” mean.
  3. A language model writes a SQL query using that context.
  4. The query is checked and run against the database, on a read-only connection.
  5. The result comes back as a table, a chart or a sentence, with the SQL alongside it.

Our analytics platform works this way: a question goes in, and the chart and the SQL that made it come back, ready to add to a dashboard.

Where it goes wrong

  • Ambiguous terms. “Customers” might mean accounts, contacts or anyone who has ever paid. If the business has not defined it, the model will guess.
  • Messy models. Hundreds of raw tables with cryptic names and duplicate columns make it hard for any model, or person, to choose correctly.
  • Tricky joins. Joining at the wrong grain quietly double-counts. The answer looks plausible and is wrong.
  • Over-broad access. A model that can see everything can also query things a user should not see.

How to make it reliable

  • Model the data first. A tested, documented layer built with a tool like dbt gives the model clean tables to work with. See our data pipelines work.
  • Write the definitions down. A short glossary of metrics and terms, passed in as context, removes most of the guessing.
  • Show the SQL every time, so analysts can check it and everyone else learns to trust it.
  • Keep a set of example questions with known-good answers, and test against them whenever something changes.
  • Use read-only roles and the same row-level access rules as your other reporting tools.

Where to start

Pick one area of the business with well-understood data, such as sales or bookings, model it cleanly, and let a small group ask questions for a few weeks. The questions they ask tell you which definitions and tables to add next.

Questions people ask

Is text-to-SQL accurate?
It can be very accurate on a clean, well-documented data model with clear business definitions. On raw, messy tables it makes plausible mistakes, which is why the generated SQL should always be visible.
Is text-to-SQL safe to use on company data?
Yes, if queries run on read-only database roles with the same access rules as your other reporting, and the model only sees the schema and context it needs.
Does text-to-SQL replace data analysts?
No. It removes the queue of simple questions so analysts can spend time on modelling, definitions and harder analysis, which in turn makes the answers better.
What data warehouse does text-to-SQL work with?
Any SQL database or warehouse, including Snowflake, Redshift, BigQuery and Postgres. The model writes SQL in the dialect of the target database.

Keep reading

Start a project or WhatsApp us on +971 58 594 2391.