Questions & Answers
How to use RAG to chat with databases?
Nadav Nesher, Applied NLP Researcher, GigaSpaces answered
When people talk about “chatting with your database,” what does that really look like?
It’s simple on the surface. You type a question the same way you’d ask a coworker, like, “Which products did best in Q4?” and you get the answer back as clean data. No SQL, or guessing which table holds what, no mental gymnastics.
You’re basically using everyday language to chat with SQL, while a system behind the scenes figures out how to turn your question into a proper query. The idea isn’t to replace analysts, it’s to make basic questions painless.
And RAG is the part that makes this possible?
Pretty much, yes. An LLM doesn’t magically know your schema, it has no idea what your tables are named or how they relate to each other. RAG fills that gap by fetching the right bits of schema and feeding them to the model at the exact moment it needs them.
Without that step, the model would either guess or invent columns that don’t exist. With RAG, it stays grounded in what’s actually in your database.
Why not just give the model the schema once and move on?
Anyone who works with real databases knows the answer. Schemas drift, people add new fields after a planning meeting or someone changes a column name just because it annoys them. A new table appears because the business suddenly cares about something new.
If you hardwire this into a model or fine-tune it, you’ll be updating that model constantly, and RAG avoids that. It pulls the latest structure every time someone asks a question, so the model is always “fresh.”
What are the actual steps? How does a natural-language question become SQL?
When a user asks a question, the backend receives it and checks the schema cache. If it doesn’t have what it needs, it retrieves the schema, including tables, columns, and relationships. Then, another part of the system examines the question and determines which tables are relevant. That might sound trivial, but it’s an important filter, since you don’t want the model considering fifty irrelevant tables, which would considerably slow response time
Once that small set is picked, the system builds a prompt that includes the user’s question along with the relevant schema. That goes to the LLM, where the model writes SQL based on what it now knows. The backend checks that SQL runs it, and sends the results back. From the user’s point of view, the whole thing feels instant.
Why does the ranking step matter so much?
Imagine giving someone a company’s entire organizational chart and asking a simple question about payroll, all that extra information would slow them down. The same thing happens with models, if the LLM sees the entire schema, it wastes attention.
Ranking narrows the focus to the most likely tables, which makes the generated SQL cleaner and more accurate. It’s one of those steps people underestimate until they see the difference.
How does this help people who don’t know SQL at all?
They get to skip the learning curve and instead, can follow their own instincts and curiosity instead of learning syntax. It also helps people who do know SQL but don’t want to write a dozen minor queries every day. Not every question needs perfect optimization, sometimes you just want an answer right now. This setup gives you that option.
People worry about hallucinations. How do you keep the model honest?
The best way to keep the model honest is by giving it real context every time. Hallucinations happen when the model has to guess. If it sees the true schema, the guesswork disappears. You can also put guardrails in place. Validate the SQL, check field names, and reject anything suspicious. If you treat this like a proper production system, the risks drop quickly.
Does this replace BI dashboards?
No, dashboards still matter, especially for repeated metrics. This system shines in all the questions that dashboards don’t cover, or can’t cover without a lengthy setup process. It’s not a replacement, it’s a pressure valve that fills the gap between fully built dashboards and raw SQL.
How does the model handle complicated joins?
Better than you might expect. If the schema shows how tables connect, the model can follow the links. It doesn’t have to “invent” relationships. Also, because the prompt includes the actual table structure, the SQL it writes tends to line up with how the data is designed. It’s not magic. It simply contains the necessary information.
What about security? People become nervous when AI interacts with a database.
Treat security reasonably. Never give the model unfettered access, and validate everything before running it. Control which tables each user can query, and mask sensitive columns. Also, log every interaction – think of it as a new interface, not a new privilege level. If you wouldn’t give someone direct database access, you don’t let this system do it either.
What happens when the SQL fails to run?
The system catches the error and tries again. Sometimes the model needs more precise directions. Sometimes the user’s question is vague. In those cases, the system can ask a simple follow-up instead of silently failing. It becomes part of the conversation.
Why is this approach taking off so fast?
Because it changes the pace of decision-making, waiting for someone to write a query slows everything; however, if anyone can ask a question and get an answer in seconds, people explore more. They make better decisions. They don’t wait for a dashboard to exist. When LLMs with RAG makes it possible to chat with SQL, the database becomes something you talk to instead of navigating.

