Teverant AI · Insights

2026-07-08

What is Text2SQL: how business users query databases in plain language, and how to deploy it

Text2SQL lets business users query a database directly in plain language, such as "Beijing sales this month," without writing SQL. This article breaks down which scenarios suit each of three implementation approaches, the engineering path from 55% to 90% accuracy, the 4 things that must pass acceptance before launch, and practical advice on ROI calculation and migrating from Excel/BI tools, helping you avoid detours and deploy quickly.

A salesperson types "Beijing sales this month": how does the system understand it?

A salesperson types "Beijing sales this month" into the workbench, and moments later a number comes back on the screen. What happened in between? A Text2SQL system has to complete four transformation steps, each of which resolves a specific kind of ambiguity.

The first step is intent recognition: the system has to determine whether this is a lookup request, a statistical need, or a comparative analysis. "Beijing sales this month" corresponds to an aggregate query that needs the SUM function; if the question were "What new orders came in from Beijing today," it would be a detail query that uses SELECT to list records; change it to "How much higher are Beijing's sales than Shanghai's," and it becomes a multi-condition comparison. This step determines the basic structure of the SQL.

The second step is entity extraction: pulling the key information out of the natural language. "This month" is the time range, "Beijing" is the geographic filter, and "sales" is the target metric. In real scenarios, people phrase things far more casually: a salesperson might say "this month," "month to date," or "the last 30 days," and the system has to interpret them uniformly as a time filter; variants such as "BJ," "Beijing City," and "Beijing Municipality" all need to map to the same region value.

The third step is schema linking, the most error-prone link in the entire pipeline. The database has an orders table, a customers table, and a products table, each with dozens of fields—so which one does "sales" actually correspond to? It might be orders.amount, or it might be orders.total_sales or transactions.revenue. Synonyms make it even harder: the business team calls it "sales," the finance system's field is named confirmed_amount, and the engineering docs say deal_value. Schema linking has to do three things: map the business term "sales" to the correct table and column; decide whether "Beijing" translates to region='Beijing' or city='Beijing'; and confirm whether the time field is created_at, order_date, or transaction_time. Get the mapping wrong, and the SQL is syntactically fine but the data it returns is wrong.

The fourth step is SQL assembly: combining the results of the first three steps into an executable statement. "Beijing sales this month" ultimately produces:

SELECT SUM(amount)
FROM orders
WHERE region = 'Beijing'
  AND create_time >= '2024-01-01'
  AND create_time < '2024-02-01'

This step also has to handle time boundaries (does "this month" mean the calendar month or the last 30 days?), null handling (should records where amount is NULL be counted?), and permission filtering (is this salesperson allowed to see nationwide data?).

A real case shows the complete flow. An e-commerce company built an internal query tool with SQLDatabaseChain, with 25 rows of test data in its orders table. A salesperson typed "How many orders are there in total" into the interface, the system automatically generated SELECT COUNT(*) AS total_orders FROM orders, and it returned 25. In this process, intent recognition determined it was a count query, entity extraction found no filter conditions, schema linking located the orders table, and SQL assembly used a COUNT aggregate. The total response time was 1.2 seconds.

But the same system can fall over with a different phrasing. Ask for "last month's sales," and the system may confuse create_time with update_time; ask for "the value of unfinished orders in Beijing," and it may miss the filter on the status field; ask for "the average order value of key accounts," and it doesn't know whether the business defines a "key account" as annual spending over RMB 100,000 or more than 50 orders.

That is why Text2SQL isn't something you can use just by plugging in a large language model (LLM) API: the semantic understanding layer needs trained intent classifiers and entity recognizers; schema linking requires maintaining a business glossary and field mapping rules; and the query generation layer must be adapted to the enterprise's database dialects (MySQL, PostgreSQL, Oracle). The technical architecture has three layers: the semantic understanding layer handles intent recognition, entity extraction, and relationship parsing; the query generation layer assembles SQL through template matching, sequence generation, or an intermediate representation; and the optimization and correction layer performs syntax checks, semantic validation, and performance optimization. Every layer has to be tuned for the specific business scenario.

From an engineering standpoint, schema linking is the optimization point with the highest return on investment. Within the same company, the sales department says "sales," the finance department says "recognized revenue," and the data warehouse field is named gmv_confirmed—three terms pointing to the same column of data. Maintaining a term mapping table so that the system knows {sales, recognized revenue, GMV} → orders.gmv_confirmed can resolve 60% of mapping errors. Then handle typos (e.g., "revenue" typed as "revnue"), abbreviations ("BJ" → "Beijing"), and multi-value matches ("tier-1 cities" → region IN ('Beijing','Shanghai','Guangzhou','Shenzhen')), and accuracy can rise from 55% to 75%.

The next question is whether to implement this pipeline with template matching, traditional NLP models, or an LLM. The approaches differ tenfold in cost, accuracy, and maintenance difficulty.

Matching three implementation approaches to business scenarios

There are three paths to deploying Text2SQL, and choosing the wrong one can sink both accuracy and cost.

The prompt engineering approach: a quick fix for weekly and monthly reports

Put the table schemas, field descriptions, and a few example queries into the prompt, and let the model generate SQL directly. This suits scenarios where query patterns are highly fixed: recurring reports such as weekly sales reports, monthly GMV statistics, and regional rankings.

The advantages are low cost and fast response: a single call consumes fewer than 2,000 tokens, and latency is typically 1–2 seconds. But flexibility is poor—if a salesperson phrases a question slightly differently ("last month" becomes "the last 30 days"), it may stop working, and the prompt template library has to be maintained by hand. Once the table structure changes, every related prompt must be rewritten.

Actual return on investment: if the team runs no more than 20 kinds of fixed reports a week, this approach has the highest ROI. The build takes 1–2 weeks, and the main work is compiling the business term mapping table (which field "sales" corresponds to, how the time range for "this month" is calculated).

SQLDatabaseChain: a half-finished solution for medium complexity

An off-the-shelf component from LangChain, it can automatically read table structures, generate SQL, execute queries, and return results. It supports aggregate queries with conditional filters and adds a layer of fault tolerance compared with the prompt approach.

Using it directly in production, however, runs into two pitfalls: first, hallucination—the model may generate SQL that is syntactically correct but semantically wrong (for example, writing a LEFT JOIN as an INNER JOIN, causing orders to be left out of the count); second, security risk—there are no permission checks or query restrictions, so in theory, if a salesperson typed "delete all orders," it would be executed.

It suits internal data analysts, used together with manual review of the SQL before execution. We don't recommend opening it directly to business users unless you add a layer of SQL review middleware (detecting DELETE/DROP keywords, limiting the number of rows per query, and logging operations).

The AI agent approach: the high-end option for data analysis scenarios

Built on LangChain SQL Agent or a similar framework, it can call the database over multiple rounds to correct errors, dynamically load the schemas of relevant tables, and adjust its query strategy based on intermediate results. When a salesperson asks "Which region is growing faster, Beijing or Shanghai," the agent first queries historical data for both, then calculates month-over-month growth, and finally generates a comparative conclusion.

Token consumption is 3–5 times that of the prompt approach: a single complex query may take 8,000–15,000 tokens, with response times of 5–10 seconds. But accuracy improves markedly: on the same test set, the prompt approach achieves 55% accuracy, while the agent approach reaches 75%–85% (and up to 90% with further engineering optimization).

Here's how to run the cost numbers: if business users make more than 20 ad hoc queries a day, and each query saves 10 minutes of manual work, then at a labor cost of RMB 200 per hour, that's RMB 66 saved per day, while the agent approach's API costs are about RMB 15–30 per day (at GPT-4 pricing)—it pays for itself in two months.

It suits data-driven teams: marketing needs to break down conversion funnels on demand, operations needs retention analysis by user segment, and product managers need to cross-compare feature usage rates. The query logic in these scenarios isn't fixed, and writing SQL by hand is slow and error-prone; the agent approach makes it possible to "state the requirement in plain language and get results in 10 seconds."

Selection decision tree

No more than 20 query types per week, fixed report formats → prompt engineering approach

Dedicated data analysts on staff, SQL needs manual review → SQLDatabaseChain + review middleware

Business teams make more than 15 ad hoc queries a day and can accept 5–10 second responses → AI agent approach

The three approaches are not mutually exclusive. A common combination in practice: use the prompt approach to cover 80% of fixed reports, use the agent approach for the remaining 20% of flexible queries, and use SQLDatabaseChain as the agent's execution-layer component without exposing it directly.

The 5 pitfalls business users most often run into

Text2SQL systems run smoothly in demos, but accuracy plummets once they meet real business scenarios. The root cause is often not model capability but the ambiguity of natural language itself carried into SQL generation. The following five categories cover most of the error cases in production.

Pitfall 1: logical ambiguity in natural language

When a salesperson says "red and blue cars," the intent is almost always color IN ('red','blue'), but the sentence structure equally allows the reading that a car must be both colors at once—two equally weighted paths as far as the machine is concerned. Similarly, "last month's top seller" could mean first place by contract value or first place by number of closed deals, which may well point to different people. The essence of the problem is that spoken language omits qualifying conditions, while SQL requires a definite value in every condition slot.

The engineering response: when the system detects logical connectives (and/or) modifying the same field, or a ranking metric that isn't unique, it should proactively ask the user a clarifying question rather than guess silently. Replace the expectation of "guessing right" with an interaction that "asks clearly," and accuracy immediately goes up a notch.

Pitfall 2: colliding meanings in field mapping

When the business says "customer," the database may have three tables—customer, client, and user—representing the contracting entity, the contact person, and the system account respectively. "Amount" is even trickier: there are three fields for the tax-inclusive price, the tax-exclusive price, and the amount actually received, and the business itself may not have thought through which one it wants. This is the core problem that schema linking has to solve: anchoring natural-language vocabulary to specific tables and columns.

A typical way this goes wrong: the system defaults to the user table, and the amount of data returned far exceeds what the business expected—because the user table includes trial accounts and internal test accounts. The business won't report an error; they'll just say "the numbers are wrong" and lose trust.

The fix is to build an explicit mapping table from business terms to database fields (a glossary), and when mapping confidence falls below a threshold, to surface the candidates for the user to choose from instead of deciding behind the scenes.

Pitfall 3: hidden assumptions about time boundaries

"This month's data"—if today is March 15, does that mean March 1 at 00:00:00 up to the current moment, or up to March 31 at 23:59:59? Is "last week" the calendar week (Monday through Sunday) or the past 7 days? Are orders that cross time zones attributed by order time or payment time?

What makes time boundary problems insidious is that whichever interpretation the system chooses, it returns a number that "looks reasonable," and the business can hardly spot the deviation from the result itself. It surfaces only during month-end reconciliation, by which time it has already affected decisions.

Engineering recommendation: fix the time semantics at the system level (for example, "this month" always means the elapsed days of the calendar month), write them into the prompt or rules engine, and display the time range actually used next to the query results so that users can verify it at a glance.

Pitfall 4: complex nested queries exceed generation capabilities

"Which employees in each department earn more than the company-wide average salary?"—broken down, this requires first calculating the company-wide average salary, then grouping by department to compute each department's average, and finally filtering and joining to specific employees. The corresponding SQL needs subqueries or nested CTEs, with a logical depth of at least two levels.

Most lightweight Text2SQL solutions perform reasonably on single-table, single-condition queries, but as soon as subqueries, multi-table JOINs, and aggregate functions are combined, the rate of correct generation drops off a cliff. In industry evaluations, accuracy on nested queries is usually far lower than on simple queries.

The pragmatic approach: for complex requirements like this, rather than forcing the system to get there in one step, guide the user to split the question into two, or encapsulate high-frequency complex queries as business views in advance, pushing the nested logic down to the database layer so that Text2SQL only needs to run simple queries against the views.

Pitfall 5: broken context causes references to fail

Salespeople tend to ask follow-up questions in quick succession: "What were Beijing's sales this month?" "What about Shanghai?" "And month over month?" Does the "what about" in the second question refer to Beijing or Shanghai? Which city and which metric does "month over month" in the third question apply to? If entity references in the conversation history aren't maintained properly, subsequent queries will draw on the wrong context and produce results that completely miss the intent.

This requires the system to be context-aware—maintaining a conversation state stack that tracks the currently active filters and entities. When a reference is unclear, inherit the subject from the most recent turn; when the topic changes, clear the historical state and start over. The implementation isn't complicated, but without it, multi-turn conversation scenarios are almost unusable.

Summary

The five pitfalls can be summed up in one sentence: natural language is inherently vague, SQL inherently demands precision, and the gap between the two is exactly what a Text2SQL system must fill with engineering. There are only three ways to fill it: a glossary for explicit mapping, a rules engine for boundary constraints, and interactive confirmation as the final backstop. None of them is advanced technology, but skip any one and you'll keep bleeding in production.

The engineering path from 55% to 90% accuracy

Hooking a prompt straight up to an LLM will certainly get you running, but the accuracy may make you question your life choices. The simplest implementation reaches about 55% execution accuracy on the Bird dataset, and even the solutions at the top of the leaderboard only just clear 77%. On complex queries—such as multi-table joins, nested subqueries, and time-window calculations—accuracy drops straight to 38.5%. If a salesperson asks for "this month's average order value for Beijing customers whose repurchase rate exceeded 30% last quarter," the system will most likely generate SQL that won't run.

The good news is that this number isn't a ceiling. By labeling a batch of real cases for your specific business scenario and combining that with engineering optimization, you can achieve accuracy above 90%—and you don't necessarily need the most expensive model.

8 optimization techniques you can apply right away

CoT reasoning: have the model break down the problem before writing SQL. When a salesperson asks "Which stores' performance is declining," the model first outputs "compare this month's sales with last month's, filter stores whose decline exceeds 10%, sort by the size of the decline," and then generates the query. This step catches a considerable share of basic errors.

Schema linking: tell the model explicitly that "sales" corresponds to the orders.amount field and that "Beijing" should be joined via stores.city. Without this step, the model tends to invent field names or join the wrong tables. You can annotate this directly in the prompt or train a dedicated field-matching module.

Few-shot examples: put several "question → SQL" pairs into the prompt. Be sure to pick cases that business users have actually asked; examples from academic datasets won't necessarily work in your business scenario. Quality matters more than quantity—one good example with a complex JOIN is worth ten simple SELECTs.

Self-consistency voting: raise the model's temperature, have it generate 5 to 10 candidate SQL queries, run them all, and the one whose results agree with the others is most likely correct. Assuming a single-pass accuracy of 80%, voting over 10 generations can improve accuracy noticeably. The cost is doubled inference spending, so it suits high-value query scenarios.

Retrieval augmentation: maintain a case library of "question → SQL" pairs, and when a business user asks a question, first retrieve the 3 most similar historical cases and put them into the prompt. In e-commerce, high-frequency questions like "this month's sales," "last month's sales," and "year-over-year sales" rarely go wrong the second time they're asked.

Revise Agent error correction: after generating the SQL, run it first; if it throws an error, feed the error message back to the model and have it fix the query. This catches syntax errors, nonexistent fields, type mismatches, and similar problems. Multiple rounds of correction can raise accuracy further.

Domain fine-tuning: label a batch of real "question → SQL" pairs from your business and use them to fine-tune an open-source model. XiYan-SQL scored 75.63 on the Bird leaderboard using multi-task fine-tuning plus continued pretraining for specific database types. In scenarios with clear boundaries, such as e-commerce order queries and customer profile analysis, fine-tuning can push accuracy above 90%.

Multi-turn conversation: when the SQL is wrong, let the business user point it out, and the system remembers the correction so it doesn't repeat the mistake. This suits teams with high tolerance who are willing to teach the system; it requires business users to invest time up front, and the problems converge after three months.

Small models can hold their own

Don't put blind faith in large closed-source models. CHASE SQL, using a 9B-parameter model and training a dedicated SQL selector, can beat Claude-3.5-Sonnet and Gemini-1.5-Pro. What matters is data and targeted training, not parameter count. If your business scenario involves just a few dozen tables and relatively fixed query types, labeling a small number of cases and fine-tuning a small open-source model won't necessarily perform worse than calling a large model through an API, and it can cut costs by an order of magnitude.

How to choose the right combination in practice

At launch, start with CoT, schema linking, and few-shot examples as your baseline—together they require no code, just prompt changes. After running for a month, collect real bad cases, identify the high-frequency error types, and then add Revise Agent and retrieval augmentation. If business users are willing, turn on multi-turn conversation so the system learns from feedback. Three months in, if volume has grown and error types have converged, consider fine-tuning a model—by then you'll already have labeled data in hand.

Complex query scenarios are still a hard nut to crack; low accuracy means most cases need a human backstop. Don't force full automation here—a semi-automated assistant is enough: after the system generates the SQL, have a colleague who knows the database take a look before executing it, which still saves most of the time spent writing queries by hand.

4 things that must pass acceptance before launch

A working technical demo doesn't mean business users can use it. A Text2SQL system must clear four acceptance gates before launch; fall short on any one of them, and business users will give up on it entirely within two weeks.

Gate 1: tiered acceptance of accuracy metrics

You can't look only at the overall accuracy figure; assessment must be tiered by frequency of use. Accuracy in core query scenarios (high-frequency questions) must reach a high level, and long-tail scenarios also need a basic accuracy guarantee. This tiered standard comes from real business feedback: business users query "sales by region this month" frequently, and one error is enough to make them doubt the system's reliability; but for an occasional complex question like "year-over-year growth versus the same period last year," 70% accuracy is already more efficient than writing the SQL themselves.

For acceptance, prepare a sufficient number of real business questions as the test set, weighted by frequency of use. The evaluation dimensions go beyond SQL syntax correctness to include result accuracy (does the generated SQL return the data the business user expected), coverage (can the system understand this type of question), and robustness (does the same question still execute correctly when phrased differently). A common trap is testing on academic datasets such as Spider, whose samples differ greatly from how business users actually express themselves; a model that scores highly may turn out to be far less usable than expected once it's live.

Gate 2: performance requirements and fallback plans

Business users won't wait. Simple queries (single table, no aggregation) must return results quickly, and complex queries (multi-table joins, subqueries) must also stay within a wait time business users can accept. Beyond that threshold, business users will conclude that "I might as well look it up myself."

Performance bottlenecks usually occur in two places: the inference latency of the LLM generating SQL, and database execution time. The former can be optimized through model selection (smaller models are sufficient for simple scenarios); the latter requires adding indexes and limiting the number of rows scanned at the database layer. More importantly, there must be a fallback plan: after a timeout, automatically prompt "This query is complex. Would you like help from a person?" or "Try narrowing the scope of your query," rather than leaving business users staring at a spinning icon.

Gate 3: a four-layer security mechanism

Text2SQL inherently carries data security risks, and four lines of defense must be set up at the system level:

  • SQL statement allowlist: allow only SELECT queries and prohibit write operations such as DELETE, DROP, UPDATE, and ALTER. Even if the model falls victim to a prompt injection attack, it cannot execute dangerous operations.
  • Sensitive field masking: fields such as salaries, national ID numbers, and mobile numbers are automatically masked or encrypted when results are returned, and the schema hints don't expose the real column names of sensitive fields either.
  • Row limits on query results: cap the number of rows a single query can return, preventing business users from accidentally exporting an entire table or overloading the database.
  • SQL injection protection: use parameterized queries and escape user input. Although SQL generated by an LLM carries a lower injection risk than traditional string concatenation, a final layer of protection is still needed at the execution layer.

These mechanisms can be implemented with function calling: wrap SQL execution in a controlled function and run security checks before each call, converting natural language into database queries safely and efficiently.

Gate 4: fault tolerance and a closed loop for bad cases

Failed execution of generated SQL (syntax errors, nonexistent fields, logic errors) is the norm, so the system must have automatic fault tolerance. The standard flow: after the first failure, retry automatically, either adjusting the schema hints (adding field descriptions or examples) or switching the generation strategy (from zero-shot to few-shot); if it still fails after several attempts, hand it off to a person and log the bad case.

The key is that bad cases must feed into an iteration loop: analyze failed samples every week, add them to the training set or use them to refine the prompt, and keep improving coverage of long-tail scenarios. For many teams, accuracy stalls after launch, and the root cause is the lack of a bad-case operations process, so the same errors keep recurring.

These four acceptance criteria correspond to the four dimensions of Text2SQL evaluation: accuracy, efficiency (generation latency and resource consumption), robustness, and security. If any one falls short, business users will go back to Excel and BI tools after the trial period, and the investment in the system will be completely wasted.

Doing the ROI math: when is it worth the investment?

The efficiency ledger: the savings are real money

For a data analyst to hand-write a multi-table join query with aggregate functions—clarifying the requirement, digging through the schema, writing the query, debugging, and getting the results—often takes a great deal of time. With Text2SQL, a business user types "the top ten SKUs by return rate in East China last quarter" and gets results quickly, including the time to revise the request and ask again. A 3–5x time difference is no exaggeration but an engineering reality: most of the time spent writing SQL by hand goes into looking up field names, working out JOIN conditions, and debugging, and a Text2SQL system does all of that for you.

For large-scale operations teams, if each person can save a meaningful amount of time otherwise spent waiting for data every day, the cumulative savings in labor costs are substantial. And that's before counting the faster response to requests that comes from "people who don't know SQL can now look things up themselves"—when a sales director needs customer segmentation data on short notice, they used to wait half a day in the data team's queue; now they ask a question and have it in 5 minutes.

Three types of scenarios where it's worth it

Query-intensive roles: data analysts, operations staff, sales, and customer service supervisors who look up data frequently each day, with relatively fixed query types (e.g., "sales by region," "retention rate for a given product," "channel conversion funnel") but filter parameters that change daily (Beijing today, Shanghai tomorrow; the last 7 days this week, the last 30 days next week). These needs can't all be configured in a BI tool (the parameter combinations explode), and writing SQL by hand is too slow—Text2SQL hits the sweet spot.

Stable schemas, flexible needs: the database table structure rarely changes, but business questions keep coming. Typical examples are e-commerce order databases, CRM customer databases, and ticketing systems—the underlying fields are stable, while the business asks from a different angle every day. A Text2SQL system trained once can be used for a long time, with diminishing marginal costs.

Moving up from Excel pivot tables: the team already uses pivot tables or simple BI dashboards but frequently hits ceilings like "a pivot table can't do this cross-dimensional analysis" or "I want to join another table but Excel freezes." Adopting Text2SQL at this point is a natural upgrade, with a much gentler learning curve than teaching them to write SQL directly.

Three traps where you shouldn't force it

Low-frequency queries: if a department looks up data only once a week, or every query is a brand-new complex analysis (such as ad hoc market research or an annual strategy report), having the data team write SQL by hand is actually more efficient—you won't even recoup the cost of labeling training data.

Rapidly changing schemas: if the database is still in a period of rapid iteration, with major table structure changes every week and fields frequently added, removed, or renamed, the Text2SQL system will be stuck in a cycle of "obsolete as soon as it's trained." In this case, stabilize the data model first, then consider an automated query layer.

Scenarios with an accuracy red line: in domains such as financial risk control, medical diagnosis, and audit compliance, which require 100% accuracy in query results, even a high Text2SQL accuracy rate leaves a residual error risk that can't be tolerated. These scenarios can only use hand-written SQL with cross-checking by multiple people, or downgrade Text2SQL to a "drafting assistant" whose final SQL must be reviewed by a person.

Cost breakdown: LLM calls aren't the big-ticket item

API fees: with GPT-4 or a Chinese LLM, the per-call cost of simple queries (single-table filters, basic aggregation) is very low, and complex queries (three-table joins, window functions, nested subqueries) cost a bit more but remain manageable. The monthly API bill for a team's routine query volume is far lower than the labor costs saved.

Labeling training data: the cold-start phase requires manually labeling a batch of <question, SQL> sample pairs, a one-time investment of some labor. After that, edge cases need to be labeled every month, which means a sustained, moderate labeling effort.

Operations costs: these include monitoring accuracy, handling user feedback, updating schema mappings, and tuning prompt templates, which require an ongoing commitment of part of some engineers' time.

The bottom line: there is a certain concentrated investment up front, after which average monthly costs are manageable, the labor costs saved far exceed the system's operating expenses, and the payback period is short. The larger the scale, the better the economics; small teams need to work out their unit economics more carefully.

Practical advice for migrating from Excel/BI tools

Most enterprises already have BI dashboards or Excel reports. Adopting Text2SQL doesn't mean tearing everything down and starting over; it means adding a natural-language entry point on top of the existing system. Rolling out in phases, running in parallel with the old tools, and preparing data and training properly can minimize migration risk.

Cover query scenarios in phases

The first phase covers only high-frequency, simple queries—requests like "this month's sales," "TOP10 customers," and "number of new orders yesterday" that involve a single table, a single metric, and a clear time filter. These queries make up the bulk of business users' daily workload, their SQL structure is simple (SELECT + WHERE + GROUP BY), LLM accuracy on them is relatively high, and they build confidence quickly. The second phase extends to multi-table joins ("TOP5 products by return rate in the Beijing region" requires joining the orders, returns, and products tables), complex aggregation ("month-over-month growth," "moving average"), and nested queries. Don't try to cover every scenario from the start; complex queries take a long time to debug and have low accuracy, and they can easily drag down the project schedule.

Run in parallel with existing tools rather than replacing them

Treat Text2SQL as an additional entry point to the BI system: business users type natural language at the top of the existing dashboard page, the system translates it into SQL, calls the existing data interfaces to return results, and reuses the existing chart components for display. When a query fails, it degrades automatically—popping up the traditional filter interface or a list of preset reports so business users can fall back on the familiar point-and-click approach. This lets people who are willing to try the new feature use it without forcing the more conservative users to change their habits. Keep the parallel period long enough, and look at usage and accuracy data before deciding whether to retire the old entry point.

Three data preparation tasks

First, map out business terms: document which database fields correspond to the terms the sales department uses every day, such as "sales," "collections," and "bad debt" (amount, payment_received, bad_debt), and which fields need to be joined ("customer name" is in the customer table, "order amount" is in the order table). Second, build a business dictionary: translate time and geographic expressions such as "this month," "last quarter," and "the Beijing region" into standard SQL conditions (MONTH(order_date) = MONTH(CURRENT_DATE), region = 'Beijing') and enter them into the system for the LLM to reference. Third, prepare few-shot examples: pick a batch of high-frequency queries from historical tickets or BI system logs, manually label the corresponding SQL, and use them as demonstration cases in the prompt. Once the LLM has seen an example like "this month's sales" → SELECT SUM(amount) FROM orders WHERE MONTH(order_date) = MONTH(CURRENT_DATE), its accuracy on new queries improves noticeably.

Train business users and set up feedback channels

Text2SQL isn't magic; business users need to know how to ask in order to get accurate results. Training should emphasize "clarity over casualness": "total amount paid in the Beijing region in January 2024" is easier to recognize than "how much money did we bring in over in Beijing last month"; when a comparison is needed, say explicitly whether it's "month over month" or "year over year," rather than leaving the system to guess. At the same time, set up an error feedback button—put a "result is wrong" entry on the query results page; when business users click it, they fill in the expected result or the correct SQL, and this feedback enters a labeling queue used to periodically update the few-shot example library and the business dictionary. The real feedback collected early after launch is the fastest fuel for improving accuracy, far more effective than tuning the model in isolation.

Migration is not a technology switchover; it opens an extra path for business users—those willing to use natural language save time, those used to point-and-click keep the original interface, and the system handles translation and fallback in between. Only by rolling out in phases, preparing data thoroughly, and following through on training and feedback can Text2SQL go from "looks fancy" to "actually useful."

FAQ: common questions about deploying Text2SQL

Could Text2SQL mess up the database or delete data?

This is the first question decision-makers most often ask, and the answer is: from an engineering standpoint, zero risk is entirely achievable, provided the security boundaries are hard-wired into the architecture design.

The standard practice is to stack three layers of protection:

  • Connection-layer restrictions—the database account used by the Text2SQL service is granted only SELECT permissions, ruling out write operations such as INSERT, UPDATE, DELETE, and DROP at the database engine level. Even if the model generates a dangerous statement, the database itself will refuse to execute it.
  • Statement filtering layer—before SQL is submitted for execution, validate it with regular expressions or AST parsing, blocking non-SELECT statements, write operations inside subqueries, and export commands such as INTO OUTFILE. This layer is the backstop, guarding against gaps in the permission configuration in extreme cases.
  • Resource isolation layer—in production, Text2SQL is usually assigned a read replica or a secondary database with replication lag measured in seconds, physically isolated from the primary database. Even if a slow query exhausts the connection pool, writes from live business operations are unaffected.

In practice, query timeouts and caps on the number of result rows are also added to keep business users from inadvertently triggering full table scans that overwhelm the replica. With these measures in place, the risk Text2SQL poses to the database is essentially no different from that of a BI reporting tool.

Accuracy can't reach 100%. What if business users don't dare to use it?

First, a reality check: hand-written SQL isn't 100% accurate either, and data analysts running their own numbers still have to check them repeatedly. The key is not to eliminate errors but to make them noticeable, correctable, and contained.

Engineering strategies for solving the trust problem:

  • Transparently show the generated SQL and the execution logic—business users don't need to understand the syntax, but the system can echo back its understanding in natural language, for example: "Here's what I understand you're looking for: the total transaction value of completed orders in the Beijing region for June 2024." Business users can spot a misunderstanding at a glance.
  • Turn high-frequency questions into templates—statistics show that most routine queries fall into a limited set of a few dozen patterns. Turn these high-frequency queries into validated templates, so that the model only needs to fill in the parameters, and accuracy can improve dramatically.
  • Add confidence markers to results—when the model has low certainty about the results it has generated (for example, when multi-table JOINs or ambiguous conditions are involved), proactively highlight them in yellow with a note: "Manual review of this result is recommended." When business users know what to expect, they won't write off the tool entirely because of an occasional error.
  • Build a closed feedback loop for corrections—after a business user flags a result as "wrong," the system logs the bad case, and the engineering team periodically collects these cases for fine-tuning or rule patches. Accuracy climbs gradually with usage, and performance after some time in production is usually much better than in the first week.

The lesson from deployment: don't wait until accuracy is high enough before rolling it out to business users. Instead, open it up first in low-risk scenarios (routine data checks, trend browsing), so that users can build trust in an environment where mistakes cost almost nothing.

Our company's database has a lot of tables. Can Text2SQL handle that?

Yes, but not by crudely stuffing the schemas of all 500 tables into the prompt. The core engineering challenge with large-scale schemas is balancing a limited context window against retrieval precision.

The common approach takes two steps:

  • Schema routing—first run a lightweight classification or vector search on the user's question to narrow a large number of tables down to the few most relevant ones, and send only those tables' field definitions and relationship descriptions to the model. The accuracy of this step directly sets the upper bound for the SQL generation that follows.
  • Business domain partitioning—group tables by business line or subject area (for example, transactions, users, logistics), with each domain maintaining its own schema documentation and few-shot examples. Questions are routed to a domain first, then matched to specific tables within it. Once tables are split by business domain, the complexity within each domain drops back to a manageable level.

Note that a large number of tables is not the biggest obstacle in itself; what's really tricky is chaotic naming (fields called c1, c2, flag_a) and missing documentation. If many fields in your database have no comments, and table relationships have no foreign key constraints and exist only as tribal knowledge, then before hooking up Text2SQL, fill in the metadata for your core tables first—the return on that investment is far higher than any fancy optimization on the model side.

Closed-source LLM or open-source model? How big is the cost difference?

There's no one-size-fits-all answer; it depends on three variables: query volume, data security requirements, and the makeup of your engineering team.

DimensionClosed-source LLM (API calls)Open-source model (self-hosted)
Startup costNearly zero; pay per tokenRequires GPU servers or inference cards; higher initial investment
Cost per queryModerate cost for complex queries (depending on the model and token usage)Lower per-query cost after hardware amortization; the higher the volume, the cheaper it gets
Data securitySchemas and query statements are sent to a third partyEverything stays within the internal network; suited to heavily regulated scenarios such as finance and government
Performance ceilingLeading closed-source models still have an edge in complex SQL generationSmall and midsize models, after domain fine-tuning, can approach or even match them in specific business scenarios
Operational burdenNo need to worry about model service stabilityYou must build your own inference service and handle concurrency and version upgrades

Practical advice: if query volume is modest and the data isn't in a heavily regulated domain, start with a closed-source API to quickly validate the value of the scenario. When query volume grows significantly, or security and compliance impose hard constraints, then migrate to a self-hosted open-source solution. Many teams follow an evolution path in which, once the former has confirmed that the need is real, they use the accumulated query–SQL pairs as fine-tuning data to train an open-source model, optimizing both cost and performance. The two paths aren't in conflict; the key is not to invest heavily in infrastructure before the value has been validated.