'How many students scored above 80 last semester' is not a retrieval question — it's an aggregate query. Embedding the database rows and searching semantically will not answer it correctly.

The pattern: schema-grounded SQL generation

Give the model the table schema (not the data), a handful of example question-to-SQL pairs, and let it generate a query. Run the query against a read-only replica, then hand the result — not the raw SQL — back to the model to phrase as an answer.

  • Never let the generated SQL run against a writable connection — read-only credentials, always.
  • Validate the query touches only expected tables before executing it.
  • Show the user the generated SQL alongside the answer; it is the fastest way to catch a wrong query.

This is a genuinely different pipeline from document RAG — see RAG vs fine-tuning: when to use which for how to decide which problem you actually have.

Pranjul Rathour, GenAI Engineer from Kanpur, India. Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at any campus: pranjulrathour41@gmail.com.