Zero Labs
Industry News / AI

Text-to-SQL Looks Solved. It Isn’t

Source · Towards AI By venkatesh babu sekar

From the publisher’s feed, collected in the ZeroLabs newsroom.

Why text-to-SQL breaks the moment it meets a real schema, and the shape of an architecture that survives it.Part 1 of 4 on building a conversational analytics engine. This part is the “why.” The next three are the “how.” ~10 min read.Every text-to-SQL demo goes the same way.Someone connects a language model to a database and types a question: “who are my top 10 customers by revenue this year.” A clean SQL query appears, the rows come back, the room nods. Text-to-SQL: solved. Ship it.Now point that same system at a real company’s data.Suddenly there are 4,000 tables instead of 12. Column names are things like STAT_CD and ORG_LVL_3. Half the relationships between tables were never written down. Three different teams have three different definitions of "revenue." The data lives in four databases that speak three SQL dialects. And every row is wrapped in permissions, because the intern asking about salaries is not allowed to see them.The demo does not survive contact with that world. And the gap between the two is not a bug you patch later. The gap is the actual problem. This series is about closing it. Before I show you the architecture I built, I want to convince you that the problem is real and specific, because the solution only makes sense once the problem does.TL;DRText-to-SQL demos look solved. On real enterprise data, accuracy falls off a cliff.The reason is not “the model needs to be smarter.” It is that several of the hardest parts are not language problems at all. They are structure, safety, and correctness problems.I will walk through seven walls every serious system hits, and why the obvious fixes (bigger prompt, few-shot, fine-tuning, a pure agent) do not fully clear them.Then the thesis for the whole series: give the model a map, draw a hard line between what’s deterministic and what’s generated, and keep the data in place.The benchmark cliff: proof the demo is lyingYou do not have to take my word that real schemas are different. The benchmarks already show it.Spider 1.0, the long-standing academic benchmark, has top systems scoring in the mid-80s for execution accuracy. But its databases are tidy: roughly 10 to 20 tables, clean names, simple logic.Spider 2.0 rebuilt the test on real enterprise databases. The same class of frontier models fell into the teens and low twenties. Same models, same week, a drop of more than sixty points.BIRD, built on real and deliberately messy databases, sits around the low 50s even for strong models, with accuracy sliding further as questions get harder.And your production numbers land 5 to 15 points below any benchmark, because real users ask messier, more ambiguous questions than benchmark authors write.A model that looks like an A-student on the demo schema is a coin flip on a real one. The intelligence did not change. The environment did.So the interesting question is not “why is the model not smart enough.” The interesting question is: what specifically is different about a real database that the model cannot handle? Here is the map of where it breaks.Where naive text-to-sql breaksHow to read it: one innocent-looking question runs straight into seven different walls, and only one of them is really about understanding language. Let me take them in order.The seven wallsWall 1: The model doesn’t know what your columns meanA schema tells you a column is named STAT_CD and holds a string. It does not tell you that STAT_CD = 'A' means "active," or that "active employee" actually requires a filter on three columns that someone encoded in application logic a decade ago.Real business meaning lives outside the schema: in tribal knowledge, in dashboards, in someone’s head.The model sees the names and the types. It does not see the meaning, because the meaning was never written down where it could read it.This is called schema linking, and it is widely considered the single hardest part of text-to-SQL: connecting a word in the question to the right table and column out of thousands. A model guessing in the dark gets it right on a 12-table toy and wrong on a real warehouse.Wall 2: It invents joins that don’t existAsk for orders by customer, and the model has to join the orders table to the customer table. On what key? If the foreign key was declared, fine. Often it was not.In the dataset I work with, only 44 of the relationships were actually declared as foreign keys. The rest are real but invisible.A model faced with a missing relationship does not stop. It guesses a join. Sometimes the guess is plausible and wrong.And a wrong join does not throw an error. It returns rows. Which brings us to the failure mode that should scare you the most.Wall 3: The fan-out trap (a wrong number that looks perfect)This one is my favorite, because it is invisible even to a careful human reviewer.Ask for total revenue per customer. The query joins customers to orders to line-items. The moment you SUM a value across a one-to-many join, that value gets repeated once per child row.A customer with one $100 order and five line items reports $500 in revenue.The SQL is syntactically perfect. The number is garbage.Nobody notices, because nothing failed.This is the defining danger of text-to-SQL: small mistakes don’t crash. They return plausible, confident, wrong answers. A system that is wrong 5% of the time, silently, is more dangerous than one that fails loudly 50% of the time. You can’t trust any of it.Cardinality correctness is not a language problem. You cannot prompt your way to certainty about it. It is a property you have to prove from the structure of the data.Wall 4: Ambiguity, in both the things and the metricsTwo more places the question is underspecified:Entities. “Bike World” might be two different stores. The honest answer is “which one did you mean,” not a coin flip that produces a confident wrong number.Metrics. “Revenue” might mean gross, net, booked, or recognized, and finance will fight about it. If the system silently picks one, it is wrong for whoever meant another.A real system needs to resolve the specific things a user names, and treat “ambiguous” as a first-class outcome that asks a question, not a problem to paper over.Wall 5: Dialect drift and data that lives in many placesPostgres, SQL Server, Snowflake, and friends all speak different SQL. Date functions, quoting, LIMIT versus TOP. A query that runs on one fails on another.And the data the question needs is often split across systems. “Compare sales to production output” might mean joining two databases that have no ability to join each other.You cannot run a cross-database JOIN that does not exist, and you cannot move all the data into one place every time someone asks a question.Wall 6: Security, the wall you cannot get wrongThis is where “democratizing data access” turns dangerous.The same system that lets anyone ask a question can let anyone ask the wrong question and get an answer they should never see.Salaries, PII, another region’s numbers: all one cleverly-worded prompt away.Here is the part that matters most: the language model can never be the thing that enforces access control. A stochastic model that “usually” applies the permission filter is not a security boundary. It is a liability with good intentions.Wall 7: Cost, latency, and the reproducibility problemEven when it works, the naive approach is expensive in three ways:Money. In a warehouse like Snowflake or BigQuery, one inefficient generated query can cost hundreds of dollars.Latency. Calling a large model for every step of every query is slow.Reproducibility. Ask the same question twice and a non-deterministic model can route two different ways. On a financial dashboard, “it depends what the model felt like” is not an acceptable answer.Why the obvious fixes don’t fully workWhen people hit these walls, they reach for four tools. Each helps. None of them, alone, clears the walls that matter most.Bigger prompt (dump the whole schema in). Helps a little with schema linking. Does not solve: it blows past context limits on real schemas, still invents joins, and still has no grain or security.Few-shot examples. Helps mimic query patterns. Does not solve: brittle, does not generalize to new questions, no safety guarantees.Fine-tuning. Helps with dialect and style. Does not solve: you often cannot train on proprietary data, it is expensive, it still hallucinates joins, and still no RBAC.A pure LLM agent. Helps with flexibility and self-correction. Does not solve: non-deterministic exactly where you need determinism (security, joins, routing).Notice the pattern. Every one of these is aimed at making the model understand better. But understanding was never the whole problem.Joins, cardinality, security, and reproducibility are not things you make a model better at. They are things you must take out of the model’s hands and prove with structure and code.That realization is the entire design philosophy of what I built.The shape of an answerSo what does a system that survives a real schema actually look like? Three ideas, and they are the spine of the rest of this series.The shape of the answer1. Give the model a map.An LLM cannot reason about what it cannot see. So before it answers anything, I build a knowledge graph plus a semantic index that captures what the tables mean, how they connect, and what is sensitive.This is a semantic layer between the question and the SQL, and it is the single biggest lever for schema linking. A graph can traverse the multi-hop relationships that vector search alone cannot find, which is exactly how you get joins right.Crucially, this map is built offline, once. The expensive thinking is precomputed, so the runtime is fast and deterministic.2. Draw a hard line between deterministic and generated.The model is genuinely great at the fuzzy parts: understanding what a question means, writing the creative slice of a query, summarizing results in prose.The model is genuinely unaccountable at the parts that must be exact: which tables you may touch, how two tables join, whether a SUM fans out.So I put those on opposite sides of a hard line. Code does routing, joins, security, and cardinality. The model does intent and the SELECT and the words. Security is never the model’s job. Joins are never the model’s job.3. Keep the data in place.The map holds metadata only. The actual rows never move into it.Queries run where the data lives, and cross-source questions get stitched together after each source has answered for itself, under its own permissions.What’s coming in this seriesEach of the next three parts builds one piece of that answer.Part 2: The domain graph. How I introspect three databases and enrich them, offline, into the map: the meaning, the join paths, and the security rules. (This is the fix for Walls 1, 2, and the schema half of 5.)Part 3: The subject graph and the query pipeline. How a plain-English question becomes a safe query: resolving the specific things a user names, the deterministic-plan / generate split, the planner that defuses the fan-out trap, and access control enforced by rewriting SQL. (Walls 3, 4, 6, 7, and the cross-source half of 5.)Part 4: RAG fusion. The forward-looking piece: extending all of this past the database into documents, so one question can draw on both a number and a paragraph.Key takeawaysThe demo is the easy 80%. Production is the hard 20%, and the hard 20% is where all the danger lives. Benchmarks built on real databases show frontier models dropping from the mid-80s to the teens.The scariest failure mode is the silent wrong answer. Bad joins and fan-out do not crash. They return confident, plausible, incorrect numbers.Most of text-to-SQL’s hardest walls are not language problems. They are structure, correctness, and safety problems, and you do not solve those by making the model smarter.The answer is architectural: a semantic map the model can reason over, a hard line between what is deterministic and what is generated, and a “data stays in place” rule.That is the problem, and the shape of the solution. In Part 2, I start building it, with the map.Further reading on the problem space: the Spider 2.0 enterprise benchmark, the BIRD benchmark on real databases, and a good field overview in this Text-to-SQL survey.Next up, Part 2: Your LLM Needs a Map !Text-to-SQL Looks Solved. It Isn’t was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.