recallA retrieval
field guide

Retrieval over Structured Data

Build this skill

Answer questions over databases with text-to-SQL safely, and combine structured and document retrieval.

Some answers live in tables, not text

"How many students were placed from CSE this year?" "Which hostel has the most vacant rooms?" "What is the average fee across all PG programmes?" These questions are not answered by any paragraph. The answers sit in structured data: database tables of placements, room allocations and fees. Searching text chunks for them either finds nothing or finds an old report with a stale number.

Structured data needs a different kind of retrieval. Instead of finding similar text, the system needs to run a query that filters, counts, adds or averages exact values. The most common way to connect natural-language questions to databases is text-to-SQL.

Text-to-SQL

SQL is the standard language for querying relational databases. Text-to-SQL means asking a language model to translate a user's question into a SQL query, running that query against the database, and then turning the result into a readable answer.

Question“placementsfrom CSE in2026?”Schema +questionto the modelSQL querygeneratedRun(read-only)exact resultAnswerwith the queryshown

The model writes the query; the database computes the exact answer.

For the model to write correct SQL, it needs to know the schema: the tables, their columns, what the columns mean, and a few example values. The prompt includes this description along with the question. The database then does the counting, so the numbers are exact, which is something a language model reading text chunks cannot guarantee.

The program below sets up a small placements database with SQLite and runs the query a model might generate for one question.

python · runs in your browser

Expected output:

text
How many CSE students were placed in 2026, and what was the average package?
SQL: SELECT COUNT(*) AS placed, ROUND(AVG(ctc_lpa), 2) AS avg_lpa FROM placements WHERE branch = 'CSE' AND year = 2026
Answer: 3 CSE students were placed in 2026, with an average package of 12.5 LPA.

The table is created and filled in memory with sqlite3, which ships with Python. generated_sql is the part a language model would write: WHERE keeps the CSE rows for 2026, COUNT(*) counts them and AVG(ctc_lpa) averages their packages. The database does the arithmetic, and the model only has to put the two numbers into a sentence.

Safety first

Letting a model write database queries is powerful, and needs firm limits:

  • Read-only access. The database account used for text-to-SQL must only be able to read, never insert, update or delete. A model can write DELETE as easily as SELECT.
  • Only the tables it needs. Expose a small set of tables or views, not the whole database.
  • Permissions still apply. A student may ask about placement statistics, but not about another student's marks. Row-level rules must be enforced by the database, not by hoping the model writes the right WHERE clause.
  • Limits. Add row limits and timeouts, so a badly written query cannot overload the database.
  • Show the query. Displaying the SQL (or a plain-language version of it) lets users and reviewers check what was actually asked.

Combining structured and unstructured retrieval

Many questions need both. "Am I eligible for the Amazon drive?" needs the student's CGPA and backlog count from the database, and the eligibility rules from the placement policy document. A good system routes the parts of the question to the right sources (a SQL query for the student's record, a document search for the rules) and gives both results to the model to write the answer.

Tables found inside documents, such as a fee table in a PDF, can go either way. If they are small and rarely queried, keeping them as Markdown tables in text chunks is fine. If people ask counting or comparison questions about them, it is often better to extract them into a real database table.

Follow your curiosity.

↑ ↓ to explore · Enter to open · Escape to close