Back to Blog
AI

"Show Me Last Month's Sales by Customer": Deploying Text-to-SQL on Company Databases with Proper Guardrails

Text-to-SQL can clear the backlog of simple data requests, but it also brings the risk of SQL that runs fine and means the wrong thing. This post covers how to raise accuracy with read-only views and a business glossary, and how to build guardrails with read-only accounts, SQL validation, and audit logs.

POLYGLOTSOFT Tech Team2026-10-117 min read9
Text-to-SQLNatural Language QueryLLMData Access ControlEnterprise Data

When Data Requests Pile Up in the IT Queue

"Can you pull last month's sales by customer?" The query behind that request is a few lines of SQL, yet once it passes through intake, prioritization, and a round of result checking, the answer often takes days. Developers get pulled off their real work several times a day, and business users give up waiting and build the numbers by hand in Excel.

Text-to-SQL shortens that queue by turning a natural-language question into SQL. Its scope, however, needs to be drawn clearly.

  • What it solves: structured lookups such as totals by period, customer, or item, plus rankings and trends
  • What it does not solve: causal questions like "why did sales drop," metrics the company has never agreed on a definition for, and judgments based on criteria that are not in the data
  • Where Text-to-SQL Goes Wrong

    The Gap Between Business Terms and the Schema

    Users say "customer," but the tables are called `CUST_MST` and the key is `BP_CD`. "Sales" may mean booked orders, confirmed shipments, or issued tax invoices, depending on the company. The model fills that gap with a guess, and the guess becomes a plausible wrong answer.

    SQL That Runs but Means the Wrong Thing

    The more dangerous failure is a wrong answer that executes without error.

  • Join fan-out: join order headers to order lines and then sum the header amount, and an order with 3 lines is counted 3 times over.
  • Missing date filter: drop the "last month" condition and you get all-time cumulative sales.
  • Missing status filter: cancelled and returned orders slip into the total.
  • Because a number comes back looking perfectly normal, the user has little chance of noticing it is wrong.

    Designing for Accuracy

    Three Kinds of Context to Provide

  • Schema descriptions: what each column means, its unit, and what its code values stand for
  • A business glossary: definitions pinned down in a sentence, such as "sales = confirmed shipments, excluding VAT"
  • Example queries: 20 to 50 pairs of frequently asked questions and verified SQL
  • Read-Only Views Instead of Raw Tables

    Rather than exposing hundreds of raw tables, it works better to expose only 10 to 20 read-only views organized around business concepts, such as `v_sales_monthly`. When the joins and the exclusion of cancelled orders are handled inside the view, the model has far less room to build a bad join, and sensitive columns can be left out from the start.

    Guardrails You Cannot Skip

  • Read-only account: grant SELECT only, and connect to a replica rather than the production database where possible.
  • Row- and column-level permissions: Sales Team 1 sees only its own customers, and cost or payroll columns are visible only to authorized users. Enforce this in the database layer, not in the prompt.
  • Pre-execution SQL validation: parse the generated SQL and confirm that it is a single SELECT statement referencing only approved views.
  • Cost and row limits: set ceilings, for example a 30-second execution time and 1,000 result rows.
  • Audit log: record who asked what, which SQL ran, and how many rows were returned.
  • Making Results Trustworthy

    A bare number does not build trust. Show the generated SQL along with a plain-language reading of it, such as "Aggregated for September 1–30, 2026, on a confirmed-shipment basis, excluding cancelled orders," so users can check the assumptions themselves.

    Quality is maintained through regular evaluation. Build a golden set of around 100 real questions paired with correct SQL, and re-score it by comparing result values whenever the prompt, the model, or the schema changes.

    When a question is ambiguous or asks for a metric that is not in the glossary, the system should ask a clarifying question or hand it off to a human owner instead of forcing an answer. Those handed-off questions then become the raw material for improving the glossary and the example queries.

    How POLYGLOTSOFT Helps

    Whether Text-to-SQL succeeds depends less on the model you pick than on how well the data has been understood and organized. At POLYGLOTSOFT, a team that understands ERP, MES, and WMS data structures builds the whole pipeline with you: designing read-only views, writing the business glossary, setting up permissions and validation, and evaluating against a golden query set. If you want to shrink your data request queue, get in touch with POLYGLOTSOFT.

    Need Technical Consultation?

    Our expert consultants in smart factory, AI, and logistics automation will analyze your requirements.

    Request Free Consultation