DEV Community

Cover image for RAG over a SQL database: why text-to-SQL is a different problem
PRANJUL RATHOUR
PRANJUL RATHOUR

Posted on Originally published at pranjulrathour.scult.in

RAG over a SQL database: why text-to-SQL is a different problem

'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.

About Pranjul Rathour

Pranjul Rathour in a grey suit at Dr. Virendra Swarup Institute of Computer Studies (VSICS), Kanpur
At VSICS, Kanpur

Pranjul Rathour in front of an Integral Startup Foundation hackathon backdrop
At an Integral Startup Foundation hackathon

Pranjul Rathour in a checked shirt inside a packed college auditorium
In a packed college auditorium

Pranjul Rathour in a suit and tie with a lanyard at a formal campus event
At a formal campus event

Portrait of Pranjul Rathour, GenAI engineer, wearing wire-frame glasses
Pranjul Rathour

Pranjul Rathour is a GenAI engineer from Kanpur, India, and CTO at SCULT INDIA, currently shipping production RAG,
fine-tuning and agentic AI systems, mentoring 200+ students through TechVerse Enclave, and judging and speaking at
student hackathons across India. Updated 2026-09-07.

Reach out if you want to talk GenAI, book a campus session, or invite him to judge:


Pranjul Rathour · GenAI engineer, 3x hackathon winner, campus mentor. Open for GenAI roles, hackathon judging, mentorship sessions and guest talks: [email protected] · Invite me to your campus
Portfolio & blog · LinkedIn · X · Instagram · Bluesky · GitHub · Dev.to

Top comments (0)