Skip to content
mentoor

Topic 3 of 4 · 4 min read

Mixing table lookups with document search

Goal: Plan the retrieval steps for a question that needs both a SQL result and a document passage, with a citation for each.

When one question needs a number from a database and an explanation from a document, plan it as two retrievals, run each against its own source, and cite each part separately. Do not push everything through one vector index.

Check yourself · 1 of 3

A user asks, "How many support tickets were open last Friday, and what is our response-time target?" How should it be retrieved?

Choose one answer

Why it matters

Real questions mix the two. "Why did our refund rate rise in March, and what does our policy say about it?" The rate is a query over an orders table. The policy is a passage in a handbook. A vector index over exported rows cannot compute a rate. A SQL tool cannot read a policy. If the system tries to do both with one tool, one half is a guess.

How to do it

  1. Split the question into needs. Write each sub-question and mark it as a table fact, a document fact, or reasoning over both.
  2. Route each need to the right tool. Numbers, counts and filters go to the SQL tool. Rules, causes and wording go to document search. The model can choose the route from tool descriptions, but a fixed plan for known question types is more predictable.
  3. Run independent steps in parallel, dependent steps in order. If the second query needs an id found by the first, wait for it.
  4. Keep the evidence separate. Store the SQL result with the query that produced it, and the passage with its document id and section. Do not blend them into one text blob.
  5. Answer with a citation per claim. The number cites the query and the date it ran. The policy statement cites the document and section. If one half is missing, say so instead of filling it in.

Before and after

Weak answer to "Is customer 4412 eligible for a refund?":

Yes, customer 4412 is eligible for a refund under our 30-day policy.

It never checked the order date. The planned version:

Plan:
1. SQL: SELECT created_at, status FROM orders WHERE customer_id = 4412 ORDER BY created_at DESC LIMIT 1;
2. Docs: search "refund eligibility window" in the support handbook.
3. Compare the order age from step 1 with the window from step 2.
Answer: Order placed 2026-02-03 [query 1]. The handbook allows refunds within 30 days [Refund policy, section 2]. The order is 47 days old, so it is outside the window.

Now every claim has a source and a reader can re-run the query.

What goes wrong

  • The model answers a table fact from memory. It writes a plausible number instead of calling the SQL tool. Require the number to come from a tool result.
  • Mismatched definitions. The database counts "refund" at payment reversal, the handbook at the request. Name the definition used in the answer.
  • Out-of-date document. The query is live and the handbook copy is a year old. Show the document's date.
  • Lost provenance. Merging both into one paragraph removes the ability to check either part.

Examples

A refund question with two sources

Question: "Can we refund order 88120?" Step one is a SQL lookup for the order date and status. Step two is a search of the refund policy for the time window and exclusions. The answer compares them and cites both. If the order is a gift card, the policy search should also return the exclusions section, so the answer can say the order is outside the policy for a different reason.

A conflicting definition

Finance asks for "churn last quarter" and the SQL tool counts cancelled subscriptions. The strategy document defines churn as cancellations plus downgrades. The combined answer reports the SQL number, quotes the document's definition, and flags that the two do not match instead of presenting one tidy figure.

Key points

  • Split a mixed question into sub-questions and mark each as a table fact, a document fact or a combination.
  • Send numbers, counts and filters to a SQL tool and rules or explanations to document search.
  • Run independent steps in parallel and dependent steps in order, passing ids between them.
  • Keep each piece of evidence with its source: the query for the number, the document and section for the passage.
  • Cite every claim, and say what is missing when one source returns nothing instead of filling the gap.

Common mistakes

  • Indexing exported table rows as text and expecting vector search to compute totals.
  • Letting the model quote a number without a tool result behind it.
  • Merging the table result and the passage into one paragraph so neither can be traced.
  • Ignoring that the two sources may define the same term differently or be dated differently.

Check yourself · 2 of 3

Why store the SQL query next to its result when building the answer?

Choose one answer

Check yourself · 3 of 3

The SQL result says an order is 47 days old and the policy passage says refunds are allowed within 30 days. What should the answer do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic