Knowledge lives in a SQL database, not documents: how does the RAG approach change?
Your answers must come from a relational database (sales figures, inventory, user records), not a corpus of text. Explain why naive vector retrieval over the rows is the wrong tool and what the RAG pattern looks like instead.
Vector search returns similar rows, but structured questions need exact aggregation. RAG becomes text to SQL: retrieve the schema and example queries, generate SQL, execute it, return the precise result.
Imagine you ask a librarian, 'how much did we earn last month?' If the librarian just hands you the three pages that sound most like your question, you still have to add the numbers up yourself — and you might get the wrong pages. What you actually want is for the librarian to walk to the ledger, run their finger down the right column, and add it up exactly. With a database, the trick is to teach the assistant the shape of the ledger (which tables and columns exist) and a few examples of how to look things up, then have it write the precise instruction the database can run. The database does the exact math; the assistant just translates your plain-English question into that instruction and reads back the answer.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Most RAG tutorials assume the knowledge lives in documents: chunk the text, embed it, retrieve the nearest chunks. So the first time someone points a RAG mindset at a relational database — sales figures, inventory counts, user records — the reflex is to embed the rows and run the same similarity search. That reflex is wrong, and understanding exactly why is what this question tests.
The mismatch is fundamental. A relational database is built to compute exact answers over structured rows: sum this column, count those rows, join these tables, group by month. The questions people ask of it inherit that shape. Vector similarity, by contrast, is built to find text that means roughly the same thing as a query. Those are different jobs, and no amount of better embeddings turns approximate similarity into exact aggregation.
This deep dive lays out why similarity search cannot answer aggregative questions, introduces text to SQL as the pattern that delegates computation to the engine that is good at it, reframes what "retrieval" even means when the data is structured, and then covers the correctness and safety machinery that makes executing model-written SQL safe in production — plus how you actually measure whether it works.
Why similarity search cannot answer aggregative questions
Start with a concrete question: "What was total revenue in Q3?" To answer it correctly, you must filter the orders table to Q3 dates and sum a revenue column. That is a deterministic computation with exactly one right answer.
Now imagine the naive RAG approach: embed every row as a vector, embed the question, and return the rows whose embeddings are nearest to the question's. What comes back is a handful of rows that are semantically similar to the phrase "total revenue in Q3" — perhaps rows that mention Q3, or rows with large revenue values. None of that is the sum. The model would then have to add up whatever rows happened to be retrieved, which is neither complete nor correct, because nearest-neighbor never guaranteed it retrieved all the Q3 rows.
The failure generalizes. Counting orders above a threshold, joining customers to their orders, finding the fastest-growing region — every one of these is a set operation or an aggregation over the full relevant rows, and similarity search retrieves an approximate subset ranked by resemblance, not the full set computed under a condition.
The takeaway is that the operation the question needs (exact computation over structured data) and the operation vector search provides (approximate semantic matching) are simply different. Better embeddings do not close that gap, because the gap is not about relevance — it is about arithmetic and set logic that similarity does not perform.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Vanna and similar tools embed schema and curated example queries, then generate SQL grounded in that retrieved context
- The Spider and BIRD benchmarks score text to SQL by execution accuracy on the returned rows, not by matching the SQL string
What an interviewer would ask next. Try answering before peeking at the approach.
QYour database has 400 tables. How do you keep the schema in the model's context manageable?
Treat schema selection as retrieval: embed table and column descriptions, and for each question fetch only the relevant tables plus their foreign-key neighbors. Add a few representative cell values to disambiguate enums. This keeps context small and accurate, and is why embeddings reappear over the schema even though they were wrong over the rows.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Embedding every row and doing nearest-neighbor search for a question like 'total revenue in Q3' — similarity returns rows that look like the question, not the exact sum, which the database could compute precisely.
60 second bullets to scan on the way to the call.
Why exact aggregative questions defeat vector similarity over rows
What the text to SQL loop does at each stage
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.