Skip to content
mentoor

Topic 3 of 5 · 4 min read

Where text-to-SQL goes wrong

Goal: Predict where a text-to-SQL tool will fail on a real schema and add the context or check that prevents it.

Text-to-SQL tools fail in a few predictable ways, and most are caused by the gap between a clean demo and your messy schema. If you know the failure modes, you can add the missing context before you ask and the right check after.

Check yourself · 1 of 3

A model writes a query that runs and returns numbers. What does that tell you?

Choose one answer

Why it matters

Demos use five tidy tables named customers and orders. Real warehouses have hundreds of tables, columns named flg_2, old copies such as orders_old, and terms only your team understands. Public benchmarks built on large, realistic databases, such as BIRD and Spider 2.0, were created because simple tests made tools look better than they are. Expect more errors on your schema than on a demo.

The usual failures

  1. Invented columns or tables. The model writes customers.signup_date when the real column is created_at. Often the database throws an error, which is the lucky case. Worse is when a similarly named column exists and is the wrong one.
  2. The wrong one of two similar tables. orders, orders_v2 and order_snapshots all look right. The model picks by name, not by which one your company trusts.
  3. Ambiguous terms. "Customer", "conversion", "active" and "revenue" mean different things to finance and product. The model picks the most common meaning.
  4. Dialect differences. DATE_TRUNC, STRFTIME, DATEADD and LIMIT versus TOP differ across PostgreSQL, SQLite, SQL Server and BigQuery. A query may fail, or worse, behave differently.
  5. Quiet misreadings of codes. A column status holding 1, 2, 3 means nothing to a model unless you explain it.

How to prevent each

  • Paste the relevant schema and say which table is the source of truth.
  • Add a short glossary: "paid = status = 'paid'; test accounts have email LIKE '%@example.com' and are excluded."
  • Name the dialect.
  • Ask for the assumptions list and check every column against the schema yourself.
  • Run the query on a small slice first and look at the rows.

Before and after

Before: "Give me conversion rate by channel." The model uses sessions and purchases, and defines conversion as purchases per session.

After: "Conversion rate = users with a first purchase divided by users with a first session, per utm_channel, using analytics.users (not analytics.users_legacy). PostgreSQL. Exclude is_internal = true. List assumptions first."

What goes wrong

  • A query that runs without errors is taken as correct. Only a syntax error is guaranteed to be caught.
  • A tool shows great results on a demo database, and the team assumes the same accuracy on the production schema.
  • The model "fixes" an error by inventing another column, and the loop continues until something runs.

When the result matters, treat generated SQL as a draft written by a fast new colleague who has never seen your data.

Examples

A plausible wrong table

Schema has orders (current) and orders_archive (before 2024). The assistant writes SELECT SUM(total) FROM orders_archive WHERE ... because the name contains the word "orders" and the earlier question mentioned history. The result is real and well formatted, and covers the wrong years. The fix is one line in the request: "Use orders for 2024 onward; use orders_archive only when I say so."

Dialect trap

-- SQL Server style
SELECT TOP 10 name FROM customers;
-- PostgreSQL / MySQL style
SELECT name FROM customers LIMIT 10;

A model that guesses the wrong dialect gives you a syntax error, which is easy to spot. Date arithmetic is harder: the same idea is written differently in each database, and a near miss can run and be wrong.

Key points

  • Text-to-SQL accuracy on demo schemas does not carry over to large real schemas, which is why benchmarks such as BIRD and Spider 2.0 use realistic databases.
  • A model can invent a column, or choose a real but wrong column or table with a similar name.
  • Ambiguous business terms are resolved by guesswork unless you supply a glossary.
  • The SQL dialect must be named, because date and limit syntax differ between databases.
  • A query that runs without errors is not evidence that it answers your question.

Common mistakes

  • Judging a tool by its demo and expecting the same accuracy on your own schema.
  • Reading "the query ran" as "the query is right".
  • Leaving similar tables, such as current and archive copies, undistinguished in the request.
  • Letting the model repair its own errors in a loop without reviewing each new column it introduces.

Check yourself · 2 of 3

Your database has orders and orders_archive. The assistant picked orders_archive for a 2026 report. What is the best prevention next time?

Choose one answer

Check yourself · 3 of 3

A tool shows 90 percent accuracy on a sample database with six tables. Your warehouse has 400 tables and unclear column names. What is the sensible expectation?

Choose one answer

Practice items, not counted towards your mastery.

Start here