How does text-to-SQL work, and how do you keep it safe?
How the model turns a question into SQL
The system sends the model three things: a description of the relevant tables and columns, the user’s question, and any business rules that apply. The model returns a SQL query. The system runs that query against the database, then returns the rows as a table or as a short explanation.
The model does this well when the schema is clear and the names are meaningful. Columns called flag_final or status_text give it little to work with, so descriptive names and a short comment on each table repay the effort quickly.
Give it read-only access and the smallest possible permission
Treat the model as an untrusted user. Connect it with a database account that can only read, never write, update or delete. Grant access to the views or tables it needs rather than the whole schema, and hide columns that hold personal data such as contact details or national identifiers. Most database systems offer roles, grants and row-level security for this. Check your database vendor’s current documentation for the exact commands.
- A read-only role with no rights to change data or schema.
- Access through curated views that expose only the columns the business has agreed to share.
- A timeout and a row limit on every query, so a poorly written query cannot hold the database or export a huge file.
- Separate roles for staff with different levels of access, enforced in the database rather than in the prompt.
Show the query and the assumptions behind the answer
Never show only the answer. Display the SQL, the date range, the filters and any definitions the model applied, next to the result. A staff member who knows the business can spot a wrong join or a missing filter in seconds when the query is visible. Keep a log of every question, query and result so mistakes can be reviewed later.
Ask the model to state its assumptions in plain words, such as which order status counts as completed. If a question is ambiguous, the system should ask a clarifying question rather than guess silently.
Write the business definitions down before anyone asks
Most wrong answers come from unwritten definitions, not from bad SQL. Does an active customer mean someone who bought within the period your team chooses, someone with an open subscription, or someone who logged in? Does revenue include refunds, tax and shipping? Each key term needs one agreed definition, stored as a view or a documented rule, so the model and the staff use the same meaning.
- Write each key term as a short definition together with the exact filter behind it.
- Name the owner who can change each definition, and record every change.
- Store the definitions where the model can read them, such as a documented glossary or a view with a clear name.
- Review the glossary whenever a product, price list or sales channel changes.
How do you check that a query is right?
Build a test set of questions your team already has answers for, such as last quarter’s figures from a report everyone trusts. Run each question through the system, compare the result, and record each mismatch with the query that produced it. Repeat the test after any change to the model, the glossary or the schema.
- Reject any query that writes, drops or touches tables outside the approved list, before it reaches the database.
- Flag queries that join many tables or scan without a date filter, and ask for confirmation before running them.
- Watch for queries that return zero rows, because a silent empty result can look like a real answer.
Common mistakes to avoid
Teams often start with the most powerful database account because it is quick to set up, and then have to unpick it later. Another common mistake is letting the model see every table, which invites it to choose the wrong one. Many teams also skip the glossary and then argue about the numbers. Each of these is far easier to prevent at the start than to fix after staff have begun to rely on the answers.
How Hundred Seas helps
Hundred Seas is a product studio in Taipei. We build AI CRM systems and AI agents on Claude and OpenAI models, and we choose each model on cost, speed, quality and where your data has to stay. For a plain-language database assistant, that means setting up read-only views, testing generated queries against reports your team already trusts, and keeping the data in the environment your rules require.
Work with us begins with a scope step of about one week at a fixed price, ending in a written plan and a firm quote. We then build in weekly releases of working software over two to four weeks, and a monthly run plan keeps the definitions, permissions and models current.
Questions
- Is text-to-SQL accurate enough for business decisions?
- It can be accurate for well-defined questions over clean, documented data. Accuracy drops when definitions are unclear, schemas are messy or questions are ambiguous. Use the answers to explore and speed up reporting, and check any figure that drives money, staffing or compliance against the official report before acting on it.
- Can text-to-SQL change or delete my data?
- It should not be able to. With a read-only role, the database itself refuses any write. Confirm this in a test by attempting an update with that account and checking that it fails. Do not rely on the prompt to stop writes, because a prompt is a request, not a control.
- Do I need a special AI product to start?
- Not necessarily. You can build a small version with a model API, a read-only database role, a list of approved views and a log table. Many teams start with one department and a short list of real questions, then widen access once the tests pass consistently.
- Which databases work with this approach?
- The pattern works with common relational databases and with cloud data warehouses. Each system uses its own SQL dialect, so the model must be told which one you use. Confirm your dialect and read the vendor’s documentation on read-only roles before you connect anything.
- What should we do first?
- Pick one team and a short list of questions they ask every week. Write the definitions behind those questions, create read-only views for the data involved, and test the answers against reports the team already trusts. Expand only after the answers match consistently.