Why LLM-Written SQL Gets Investment Data Wrong
Ask an AI model to query LSEG, S&P or FactSet tables in Snowflake and it will write SQL that runs. The result looks right. Often it is not.
What the research says
AI models have become good at SQL on small, well-documented databases. On financial databases they still get the numbers wrong, and the evidence from 2026 is direct.
One 2026 result seems to say otherwise. dbt Labs measured AI-written SQL at 84% to 90% accurate, and that figure gets quoted a lot. It comes from a small sample database with readable names, and it does not apply to investment data. The studies on financial data come first, then the reasons.
On investment and financial data
- FLINT (October 2026, EMNLP Industry Track) ran seven leading text-to-SQL methods on two production financial datasets. More than 99% of the queries ran without error. No method got more than 50% of the answers right. The authors point to the cause: concepts are stored as opaque ID numbers rather than readable names, so the query runs but filters on the wrong value.
- FINCOSMOS (November 2026) asked four coding agents, including Claude Code and Codex, to build investment backtests from data they gathered themselves. Programs then checked each result against point-in-time rules and recomputed every reported figure. Across 150 sessions, none passed. Every session the user had signed off on failed at least one of those checks. The authors call these silent failures: a backtest that uses the future produces a report as convincing as a correct one.
On enterprise data in general
- EntSQL (June 2026) tested models on business questions that need company knowledge beyond the schema. The best system reached 15.9% on English questions, even with the documentation supplied.
- Rumiantsau and Fokeev (April 2026) ran 100 questions past Claude Opus 4.7, Claude Sonnet 4.6 and GPT-5.4. From the schema alone, the models scored 45.5% to 50.5%. Given a short document describing what the data means, they scored 67.7% to 68.7%.
Where models have improved
It would be wrong to say AI cannot write SQL. Spider 2.0, a benchmark of 632 enterprise SQL problems, launched in 2024 with the best agent solving 21.3%. By late 2026, purpose-built agent systems were reported above 90% on its Snowflake track.
The result quoted most often comes from dbt Labs. In April 2026 it reported current models writing SQL directly at 84% to 90% accuracy, up from 32.7% for GPT-4 on the same questions in 2023.
Why the dbt Labs result does not apply to investment data
An asset manager who sees “90% accurate” could fairly assume it covers their own databases. It does not. The number is real, but look at what was tested:
- A small database. The benchmark is a sample insurance company, ACME Insurance, with about 15 tables. A single LSEG, S&P or FactSet feed has thousands.
- The whole schema in the prompt. Every table definition was handed to the model up front. dbt Labs notes that this is not practical for larger datasets.
- Readable names. The tables are called
Claim,PolicyandPremium. The columns are called things likeClaim_Open_Date. Nothing is stored as a vendor code that has to be looked up, which is the exact problem FLINT found on real financial databases. - Simple questions. There are 11, such as “How many claims do we have?” None asks what was known on a past date, converts a currency, or tracks a security through a ticker change.
- Tables tidied first. On the original tables the same models scored 64.5%. They reached 84% to 90% only after three new tables were added that pre-joined the raw ones.
dbt Labs draws the same line itself. It recommends direct SQL for ad hoc work and smaller datasets, and a semantic layer where accuracy is critical and the data is large, complex or messy. Through that layer the same models scored 98% to 100%. And when the layer could not answer, it said so. In the authors’ words, “Text-to-SQL will cheerfully give you a wrong number.”
Investment data sits on the other side of every one of those points. The closest like-for-like test is FLINT: the same generation of models, on production financial schemas, with no method above 50%.
What the studies agree on
What has not changed is the pattern. Accuracy is high when the schema is small and its meaning is written down. It falls when the database is large, the identifiers are opaque, and the rules about dates live only in the heads of the people who use it. Investment data is that second case. And in every study the fix is the same: give the model a layer that already encodes what the data means.
A worked example
The request: trailing twelve-month sales in US dollars for every S&P 500 company, at each month-end from 2015 to 2024.
A model writing SQL from scratch has to get all of this right:
- The universe. Which companies were in the index on each of 120 month-ends, not which are in it today.
- The item. Which of thousands of fields is quarterly sales, and in which table.
- The quarters. Which four quarters were the latest known on each date, given that results are published weeks after the quarter ends.
- The currency. Which companies report in something other than dollars, and which exchange rate applies.
- The security. How to connect an index member to its fundamentals when tickers change and are reused.
Miss any one and the query still runs. The table still has 500 rows per month. The numbers are simply wrong, and nothing says so.
The five common failures
- Invented item codes. The model guesses a field name that sounds plausible and does not exist, or picks a similar item with a different definition.
- Look-ahead bias. It joins on fiscal period end instead of the date the figure was known.
- Misaligned dates. Daily prices, quarterly fundamentals and monthly estimates are joined without a rule for which value applies on which day.
- Mixed currencies and units. Figures in different currencies, or in thousands and millions, are compared as if they matched.
- The wrong security. History gets attached to the wrong company.
Why prompting does not fix it
These are not reasoning mistakes. They are missing knowledge: thousands of vendor tables, each with its own identifiers, calendars and restatement rules. A longer prompt does not supply that.
The same request through MDO
MDO has already encoded those rules. The agent looks up the real codes with catalog_search, then writes this:
import mdosnow as mdo
days = mdo.DateRange(startDate='2015-01-31', endDate='2024-12-31',
periodType='month', by=1, dom=31)
universe = mdo.ConstituentUniverse(items='DS_CONST_M__S&PCOMP', days=days)
mdo.FundamentalData(universe=universe,
items={'SALES_TTM_USD': 'WS_1001_Q'},
ttm=True, toCurrency='USD')
result = universe.return_df()
DS_CONST_M__S&PCOMP is the S&P 500’s monthly constituents from LSEG Datastream. WS_1001_Q is quarterly net sales from LSEG Worldscope. Both come from the same LSEG feed, so the index members and the fundamentals line up.
Historical constituents, the four latest known quarters, currency conversion and security mapping are all handled inside those calls, the same way every time. Written by hand, the trailing twelve-month step alone is about 200 lines of SQL.
The model handles the conversation. MDO handles the data. See MDO for AI agents for the tools and when to use them.
Sources
- Sehanobish et al., From Benchmarks to Production: A Text-to-SQL System for Complex Financial Data, EMNLP Industry Track, October 2026
- Cao et al., FINCOSMOS: An Open-World Evaluation Paradigm for Agents in Finance, November 2026
- Liao et al., EntSQL: A Benchmark for Grounding Text-to-SQL in Long-Context Enterprise Knowledge, June 2026
- Rumiantsau and Fokeev, Semantic Layers for Reliable LLM-Powered Data Analytics, April 2026
- Ganz and Perigaud, dbt Labs, Semantic Layer vs. Text-to-SQL: 2026 Benchmark Update, April 2026
- Sequeda, Allemang and Jacob, A Benchmark to Understand the Role of Knowledge Graphs on Large Language Model’s Accuracy for Question Answering on Enterprise SQL Databases, 2023
- Lei et al., Spider 2.0: Evaluating Language Models on Real-World Enterprise Text-to-SQL Workflows, ICLR 2025. Late-2026 scores as reported by nl2sql.ai, October 2026
These studies did not test MDO. They are cited for what they show about AI models and enterprise data.