EnableYourData.nl

Resources · AI

RAG or text-to-SQL: which AI suits your business data?

RAG and text-to-SQL are two ways to make AI work with your own data. RAG (retrieval-augmented generation) searches your documents for relevant text passages and lets the model formulate an answer from them; text-to-SQL lets the model write a query on a database, so that the database does the calculating. Use RAG for questions about documents and knowledge, and text-to-SQL for questions about figures.

Key takeaways

  • RAG is designed for unstructured data, such as documents, handbooks and contracts.
  • Text-to-SQL is designed for structured data in a database, such as revenue, stock and orders.
  • With RAG, the model formulates the answer; with text-to-SQL, the database does the calculating and the model only writes the query.
  • You can combine both by routing each question to the right technique.
  • ENABLE uses only text-to-SQL; RAG on documents is not part of the platform.

Two ways to make AI work on your own data

A language model does not know your organisation. It does not know what your revenue was, what your purchasing terms say or which customer is behind on payments. To make AI useful on your own data, you have to give the model the right information at the moment the question is asked.

There are two common techniques for this. RAG (retrieval-augmented generation) retrieves relevant pieces of text from your documents. Text-to-SQL lets the model write a query on your database. A third route, further training a model on your own data (fine-tuning), is rarely the first choice for factual questions: a model does not reliably remember facts and has to be retrained after every change.

How RAG works

RAG is the usual technique behind an AI knowledge base in a company: an assistant that answers questions about the staff handbook, quality procedures, contracts or support articles.

With enterprise RAG, meaning RAG at the scale of an organisation, extra requirements come into play. Permissions per document must be carried over into the search index, so that nobody can use the AI to see a document they do not have access to. Outdated versions must be removed, source references must be accurate and you need to measure regularly whether the answers are correct. Also bear in mind that with RAG, text passages from your documents are sent to the language model. In broad terms, RAG works like this:

  1. Documents are split into smaller pieces of text (chunks).
  2. An embedding is created for each piece: a series of numbers that captures its meaning. These embeddings go into a vector database or search index.
  3. When a question comes in, the system looks for the pieces that resemble it most, often combined with ordinary keyword search.
  4. The pieces found are sent to the language model together with the question.
  5. The model formulates an answer and refers to the sources.

How text-to-SQL works

With text-to-SQL, the model receives the structure of your database: tables, columns, relationships and descriptions from a data dictionary. Based on that, it writes an SQL query, which the database runs. The answer to "What was the margin per product group?" therefore does not come from the language model, but from your own database.

This makes text-to-SQL strong at exactly the work language models are weak at: exact calculations across thousands or millions of rows. The result is verifiable, because you can read the query. The quality depends mainly on a clear data model and good metadata.

RAG and text-to-SQL compared

Both techniques give AI access to your own data, but they differ in almost everything: the type of data, what is sent to the model and how you check an answer.

RAGText-to-SQL
Type of dataUnstructured: documents, emails, wikisStructured: tables in a database or data warehouse
Typical question"What is our notice period for suppliers?""What was the margin per product group in the second quarter?"
What the model receivesText passages from your documents and the questionMetadata (tables, columns) and the question
Who supplies the answerThe language model, based on the passagesThe database; the model only writes the query
Verifiable throughSource references to the documentThe query that was run
Biggest riskWrong or outdated passages, yet a confident answerA query that is technically correct but answers a different question
Calculating across many rowsWeakStrong
Access rightsCarried over per document or folder into the search indexThrough database permissions, views or filters
Main preparationClean up documents, keep them current, record permissionsGet the data model and data dictionary in order

When to choose RAG and when to choose text-to-SQL

The rule of thumb is simple: if the answer is in a text, choose RAG; if the answer has to be calculated from tables, choose text-to-SQL.

A common mistake is using RAG on exports of tables, such as a CSV with all orders in a vector database. The system then finds a handful of rows that resemble the question, and the model calculates with those. A total across all orders cannot be reliable that way. This is how to make the choice:

  • Choose RAG for questions about policies, procedures, contracts, product documentation, quotes and minutes.
  • Choose text-to-SQL for questions about revenue, margins, stock, orders, hours, planning and other KPIs.
  • If in doubt, look at the type of answer: an explanation or quotation points to RAG, a number or table to text-to-SQL.

Combining RAG and text-to-SQL

Many questions in an organisation touch on both types of data. "Why did revenue fall in the North region?" calls for figures from the database and context from reports or notes. A combined solution sends each part of the question to the right technique: a routing layer decides whether a query is needed, a document search, or both.

RAG can also help text-to-SQL. With large databases containing hundreds of tables, you can first use RAG to look up the relevant table descriptions in the data dictionary, and only then let the model write a query. Complexity does increase, though: you have two security models, two sources of errors and a harder evaluation. So start with the technique that fits your most important questions.

What ENABLE does and does not do

ENABLE uses text-to-SQL. In the SQL Explorer, you ask a question in plain language; the AI writes SQL on your organisation's PostgreSQL data warehouse and ENABLE runs it read-only. The AI receives only metadata and the question, never rows of data.

ENABLE does not do RAG: the platform does not search documents, SharePoint sites or knowledge bases. If you also want to answer questions about documents, you need a separate RAG solution for that. You can, however, make saved queries available as a REST endpoint through the Data API, so that other systems can retrieve the same verified figures.

FAQ

Frequently asked questions

What is RAG?

RAG (retrieval-augmented generation) is a technique in which an AI system first looks up relevant text passages in your own documents and passes them to a language model together with the question. The model bases its answer on those passages and can refer to the source. RAG is widely used for AI knowledge bases and assistants that work on documents.

What is the difference between RAG and text-to-SQL?

RAG searches for text passages in documents and lets the language model formulate an answer from them. Text-to-SQL lets the model write a query on a database, after which the database does the calculating. RAG suits questions about documents, text-to-SQL questions about figures.

Can I use RAG for questions about revenue or stock?

That is not recommended. RAG finds a limited number of text passages and lets the model calculate with them, which makes totals across many rows unreliable. For figures from tables, text-to-SQL is more suitable, because the database does the calculating.

What is enterprise RAG?

Enterprise RAG is RAG at the scale and with the requirements of an organisation. Think of access rights per document in the search index, current versions only, reliable source references, logging and regularly measuring the quality of the answers.

How do you build an AI knowledge base for your company?

An AI knowledge base is usually built with RAG. You collect and clean up the documents, record who may see what, put them in a search index and connect that to a language model that answers with source references. Start with a well-defined set of documents and test the answers with the people who know the content.

Does ENABLE support RAG?

No. ENABLE uses text-to-SQL on structured data in your data warehouse and does not search documents. For questions about documents, you need a separate RAG solution.

Share securely, live fast, no headache

In an online demo we show you the portal: dashboards per role, row-level security, plain-language questions and how we set it up and manage it for you.