Every growing company has the same bottleneck. The sales head wants to know which region had the most repeat orders last quarter. The operations lead wants to see how many tickets took more than two days to close. The founder wants a quick cut of revenue by plan. All of that data already sits in a database, yet each question goes into a queue and waits for the one person who knows SQL.
Text-to-SQL changes that. Large language models are now good enough at writing SQL that a business user can type a question in plain English and get an answer back in seconds. Done carelessly, it is also an easy way to leak data, overload a database or hand people confidently wrong numbers. This guide walks through how to build a text-to-SQL assistant that is useful, safe and trustworthy for a small or mid sized business.
At its simplest, a text-to-SQL assistant follows a short loop:
The model call is the easy part. The real engineering work lives in the context you give the model, the guardrails around execution and the way you measure whether the answers are correct.
Most SMEs and startups do not have a full analytics team. They have one or two people who understand the data model and dozens who need answers from it. Dashboards solve the recurring questions, but the long tail of follow up questions ("ok, now split that by city") never ends. A text-to-SQL layer gives non technical staff a way to explore without filing a request, and frees technical people to work on harder problems.
It also pairs naturally with a reporting stack you may already have. If you have set up a read replica for reporting, as described in our guide to startup dashboards with Metabase and a Postgres read replica, you already have the safe data source a text-to-SQL assistant should point at.
Consider a hypothetical regional distributor with a custom order management system built on Postgres. The data includes orders, order items, customers, products, sales reps and territories. The sales team constantly asks the same developer for numbers: top customers by territory, slow moving products, reps who have not logged a visit in two weeks.
A practical first version for a business like this would:
In a setup like this, the developer could plausibly move from answering several data requests a day to reviewing a weekly log of questions the assistant struggled with. That log then becomes the roadmap for improving definitions and examples. The numbers here are illustrative, but the pattern is common: the value comes less from the model and more from finally writing down what your business terms actually mean.
Do not start with your entire database. Pick one domain, such as sales or support, where people ask frequent ad hoc questions and where the tables are reasonably clean. A narrow scope makes accuracy far easier to achieve and to measure.
Raw production tables are full of legacy columns, soft deleted rows, internal flags and confusing names. Create database views that present a clean picture: consistent names, only the columns people need, and filters already applied (for example excluding test orders and deleted records). The model then reasons about a simple, honest model of the business instead of your implementation details.
This is the step most teams skip, and it is the one that matters most. What counts as revenue: gross or net of refunds? Is a customer "new" based on signup date or first order date? Which time zone do reports use? Put these definitions into the context you send to the model, next to the schema. If two departments disagree on a definition, the assistant will expose it quickly, which is a useful side effect.
Few-shot examples improve results noticeably. Write a set of realistic questions with correct SQL, covering joins, date filters, grouping and the definitions above. As your library grows, retrieve only the most relevant examples for each new question rather than sending all of them every time.
Security is not optional here. At minimum:
Asking the model for a structured response (the SQL, a short explanation and a confidence flag) makes validation much easier. Our guide on getting reliable JSON with structured outputs covers the technique in detail.
Collect 50 to 100 real questions from the team, along with the correct answers. Run the assistant against them and compare results, not just SQL text, because two different queries can produce the same correct answer. Track the pass rate every time you change prompts, examples or models. Without this, you have no idea whether a change made things better or worse.
Show the generated SQL behind a "view query" toggle. Show which tables and filters were used in plain language ("Orders from 1 to 31 October, excluding cancelled orders"). Let users flag wrong answers with one click. When the model is unsure, it should ask a clarifying question instead of guessing.
Log every question, generated query, result size and user feedback. Review the failures weekly. Most fixes will be adding a definition, renaming a confusing column in a view or adding an example, not changing the model.
If your data is scattered across spreadsheets with no consistent structure, start by consolidating it. If the questions people ask are always the same five, a well designed dashboard is cheaper and more reliable. And if your data includes highly sensitive personal information, think carefully about whether natural language access is appropriate at all, and for whom.
The best text-to-SQL projects are really data clarity projects with an AI interface on top. The model is only as good as the definitions you give it.
When we scope AI data assistants with clients, we usually spend more time on the semantic layer and the evaluation set than on prompts. That keeps the first release small, measurable and safe, and gives the team a clear way to expand coverage one domain at a time. If you are exploring AI features like this for your own product or internal tools, our AI development services page explains how we typically structure that work.
Text-to-SQL can turn your database from something only engineers can question into something the whole team can explore. The recipe is consistent: start narrow, curate clean views, write down your business definitions, lock execution to read-only and limited queries, and measure accuracy against real questions before and after every change. Treat it as a living system that improves with feedback, and it can become one of the most used internal tools your team has.