Text-to-SQL Agents: Why Valid SQL Can Still Produce the Wrong Answer
Evaluate text-to-SQL accuracy beyond syntax, with weighted-rate examples, semantic checks, permission boundaries, and a practical regression suite.
Text-to-SQL translates a question into a database query. A query that parses and runs has passed a technical check; it has not necessarily answered the intended business question.
To evaluate accuracy, check the chosen population, metric, time range, grouping, and execution scope. Then check whether the explanation describes the returned result faithfully.
The query runs, but the rate is wrong
Consider two fictional acquisition channels in the same period:
| Channel | Converted visitors | Eligible visitors | Conversion rate |
|---|---|---|---|
| Referral | 1 | 2 | 50% |
| Search | 9 | 90 | 10% |
An agent asked for the overall conversion rate might average the two percentages and return 30%. The correct aggregate is 10 conversions divided by 92 eligible visitors, about 10.87%.
Given a table with one row per channel and the columns shown below, this query calculates the combined rate:
SELECT
100.0 * SUM(converted_visitors)
/ NULLIF(SUM(eligible_visitors), 0) AS conversion_rate_pct
FROM channel_conversions;
This assumes visitors belong to one channel in the report and the conversion definitions are aligned. If the same visitor appears in several channels, the denominator may need deduplication at visitor grain before aggregation. Correct arithmetic cannot repair the wrong population.
Separate five kinds of correctness
- Syntax: can the warehouse parse the query?
- Structure: are the tables, keys, joins, and data types valid?
- Semantics: is the metric calculated over the intended population and time convention?
- Authorization: may this user execute this operation and see these results?
- Communication: does the explanation preserve units, scope, uncertainty, and evidence?
Treat them as separate checks. A model can pass the first two and fail the last three. A database error rate alone is an incomplete quality metric.
Give the agent a smaller query surface
Expose curated views or an approved metric interface when the task is routine reporting. Provide business definitions, supported dimensions, and example questions. Ask for clarification when a term maps to several legitimate metrics.
A semantic layer can support that process, but it must be connected to the execution path. Supplying metric descriptions while letting the agent freely bypass them through raw SQL leaves the definitions advisory.
Some systems intentionally support open-ended SQL investigation. In that case, define allowed datasets, resource budgets, and validation steps. A text check for a leading SELECT is not a sufficient security boundary. Use appropriate query parsing, restricted database credentials, and platform-enforced limits.
Design tests around expected behavior
Build small fixtures with known answers. Include the rate example, duplicate join keys, a one-to-many relationship, null amounts, an empty result, and timestamps around a reporting boundary.
Add questions with equivalent wording and questions that should produce different answers. “Orders created in August” and “orders paid in August” are not interchangeable unless the data and business definition make them so.
For a period with no eligible visitors, specify whether the answer should be unavailable rather than zero. The query above returns null for a zero denominator. The explanation should preserve that distinction.
Test access using at least two users with different scopes. Verify the permitted result set and whether restricted rows can affect exposed aggregates. Keep query results and traces within the same confidentiality boundaries as the underlying data.
Compare results, not identical SQL strings
Different queries can produce the same correct answer. Conversely, the same SQL can become wrong after a definition or source schema changes. Evaluate known outputs on fixtures, inspect query properties that matter, and reconcile representative real requests against an approved calculation.
Record the question, definition version, query, data snapshot, and answer. Re-run the suite when changing models, prompts, tools, joins, or metric definitions.
For related errors, use our AI data-answer diagnosis guide. Discuss an AI analytics implementation with Labs4Change if you need to turn a successful SQL demo into a dependable reporting tool.
Keep reading
2026-09-12
Why AI Gives Wrong Answers About Your Business Data—and How to Diagnose the Problem
Trace incorrect AI analytics answers to metric definitions, joins, time ranges, missing data, or unsupported explanations, using a worked example.
2026-09-24
How to Build an AI Data Agent Over Your Existing Data Warehouse
Design a first AI data agent using a narrow business task, governed metric tools, authenticated access, traceable answers, and acceptance tests.
2026-09-15
How to Build a Semantic Layer for AI: Metrics, Relationships, and Business Rules
Build a small, testable semantic layer for an AI data agent, from a metric contract and order-level SQL to access controls and acceptance checks.