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

> Source: <https://pub.towardsai.net/text-to-sql-with-rag-building-a-chatbot-that-talks-to-your-database-1938f93f31ca?source=rss----98111c9905da---4>
> Published: 2026-08-01 22:01:01+00:00

Member-only story

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

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