cd /news/artificial-intelligence/text-to-sql-with-rag-building-a-chat… · home topics artificial-intelligence article
[ARTICLE · art-83312] src=pub.towardsai.net ↗ pub= topic=artificial-intelligence verified=true sentiment=· neutral

Text-to-SQL with RAG: Building a Chatbot That Talks to Your Database

A hands-on proof of concept demonstrates how retrieval-augmented generation (RAG) can be applied to structured data by converting plain English questions into safe, verified SQL queries, using Python, SQLite, and an OpenAI API key, with the full source code available open-source. The author, who built the system, highlights that the initial version often produced incorrect answers without indicating uncertainty, and the post focuses on addressing that gap.

read1 min views1 publishedAug 1, 2026
Text-to-SQL with RAG: Building a Chatbot That Talks to Your Database
Image: Pub (auto-discovered)

Member-only story

How to use RAG with structured data — a hands-on POC that converts plain English into safe, verified SQL, with real examples of what breaks and how to fix it.

Almost every RAG example you see online uses unstructured data. PDFs, wikis, support articles — chunk them, embed them, retrieve them, done. But here’s the thing: most of the data a business actually cares about doesn’t live in documents. It lives in relational databases — customers, orders, invoices, products.

So I set out to answer a simple question: how does RAG work with structured data? Can a chatbot answer questions from a database just as naturally as it answers from documents — so that one day, the same bot can pull from both?

This POC is my answer. The core technique is text-to-SQL: the user asks in plain English, and the system converts it into a safe, correct SQL query. It sounds simple. The first version I built always gave an answer — it just wasn’t always the right answer, and it never told me when it was guessing. Everything in this post is about closing that gap.

The full source code is open — link at the end. Everything runs locally with Python, SQLite, and an OpenAI API key. No vector database, no Docker, no cloud setup.

── more in #artificial-intelligence 4 stories · sorted by recency
── more on @openai 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/text-to-sql-with-rag…] indexed:0 read:1min 2026-08-01 ·