Text2SQL in analytics: how we taught AI to understand business requests without intermediaries

Mikhail Kulyaskin23 Sep 2025

Problem

Managers at a large retailer wait for analysts to pull numbers such as the average check by city. The team built a chat assistant that turns a plain question into SQL and returns a number, a table or a chart. Real questions are short, use abbreviations and legacy terms, and often lack a period or metric. Benchmark metrics fit badly: a correct answer with one extra column counts as wrong. The first version covered one mart of about 150 columns and several billion rows.

Example from the article: "Accrued points for plant H495" uses the legacy word "plant" for a store.

Solution in brief

The pipeline rewrites each question with the dialogue history, filters off-topic or vague input and asks a clarifying question when needed. Qwen2.5-72B then writes SQL from the schema, metric formulas and few-shot examples, and re-checks it. Failed queries are regenerated with the error message. On 150 questions this gave about 76% execution accuracy. Schema linking and multi-model candidate selection were tested and set aside.

Questions this case answers
  • How to build a natural-language question-answering assistant over a large table so that non-technical users get a text, table or chart answer without a specialist
  • How to evaluate generated queries against reference queries when strict result matching marks correct answers as wrong
  • How to pick the generation strategy for a query-writing LLM when accuracy gains from ensembles cost several times more hardware and latency
  • How to let managers get numbers from a billion-row table by asking in plain language instead of waiting for an analyst?

Tools and methods

  • Qwen2.5-72B — SQL generator, second best in quality at far less hardware than DeepSeek R1.
  • Few-shot examples — nearest question–SQL pairs from a vector store (multilingual-e5-large).
  • Execution Accuracy and LLM-as-a-judge — lenient result match plus a GPT-4o comparison of SQL.
  • Vanna — open-source baseline.
Continue reading — it's free

Google or a one-time email code. No password.