Guide · Text-to-SQL

Why your text-to-SQL agent picks the wrong table (and how to fix it)

For teams building a data agent over their own warehouse.

Updated

A text-to-SQL agent picks the wrong table when the business meaning of your data lives in people's heads, not in the schema. The fix is a context layer: a Brain that records which table backs each entity, what your terms mean, how entities join, and which rules apply, and that the agent reads before it writes a query.

  • Most wrong-table errors fall into six repeatable failure types.
  • Each one maps to something the Brain stores: entities, vocabulary and aliases, relationships with join keys, rules, field annotations, metrics.
  • The agent still runs queries against your warehouse with your credentials; the Brain supplies context.

The problem is missing meaning, not a weak model

A schema tells an agent what exists, not what it means. Which of three order tables is the real one, what "active" means, which join is safe, and which rows to exclude are facts about your business. If they live only in a few heads, the agent guesses, and guesses differ run to run.

Read the context before writing the query

Connect the schema (and your docs) once and the Brain holds the answers as typed records. The agent asks for them over MCP. get_index returns the overview, then recall_domain takes a kind and a name. For a concept it returns the entity with its relationships, including cardinality and join keys; for a rule, the rule text and its conditions; for a metric, its grain entity, unit, kind and origin.

{ "kind": "concept", "name": "Order" }
{ "kind": "rule" }
{ "kind": "metric", "name": "Net revenue" }

Where each answer comes from

Entities, fields, keys and relationships come from the schema you connect. Definitions, terms and rules come from the docs you add, such as a data dictionary or runbooks. A person approves what ingestion proposes, and curated values survive re-ingest. Aliases record synonyms, including a column whose real name has a typo. Nothing here needs rows: by default only schema metadata is read.

What this does not do

The Brain does not execute SQL and does not guarantee a correct query. It gives the agent the definitions, join keys and rules to write one. Enforcement of business policy stays in your own harness; the Brain holds the rules content.

Six ways an agent picks the wrong table, and what fixes each
FailureWhat happensWhat the Brain stores
Look-alike tablesorders, orders_v2 and orders_archive all seem rightAn entity per business concept, with runtimeBinding recording where it lives
Your term is not a column name"active customer" has no column called activeVocabulary terms mapped to entities, and aliases
Wrong join pathIt joins on a column that looks right but is not the keyRelationships with cardinality and join keys
Unwritten rulesIt counts test accounts or refunded ordersRules with their conditions
Unclear column meaningIt reads cents as dollars, or an enum code as a labelField descriptions, annotations for units and encodings, optional column profiles
Metric driftRevenue is computed differently on each runA metric definition: grain entity, expression, default time field

FAQ

Why does my text-to-SQL agent choose the wrong table?
It sees table and column names but not your meaning. Several tables look right, your terms do not match column names, and rules such as "exclude test orders" are written nowhere the agent can read.
Does BrainBox run the SQL?
No. Your agent runs queries against your warehouse with your credentials. BrainBox supplies the definitions, join keys, rules and metrics.
What does the agent call to get this context?
recall_domain, with a kind such as concept, term, rule or metric and a name. get_index gives the overview first.
Are database rows sent to BrainBox?
Not by default. Opt-in column profiling (--profile-data) samples rows for statistics, and full mode also sends enum values and JSON key shapes.

Give your agents a brain.

Sign in and ask your first question.

Book a Demo