04. SQL RAG¶
Category: Advanced RAG Architecture
Module: Part V โ Advanced Retrieval-Augmented Generation
Difficulty: Advanced
๐ Overview¶
Traditional RAG systems primarily retrieve information from unstructured content such as:
Vector retrieval works particularly well when the user asks:
"What does the refund policy say?"
"Explain the authentication architecture."
"Summarize the incident report."
However, enterprise data is also heavily structured.
Examples include:
Customers
Accounts
Transactions
Orders
Products
Employees
Invoices
Payments
Inventory
Subscriptions
Metrics
This information typically lives inside relational databases.
For these questions:
"How many customers signed up last month?"
"Which products generated the highest revenue?"
"Show the top 10 customers by transaction volume."
"How many failed payments occurred yesterday?"
semantic vector retrieval is usually not the right primary retrieval mechanism.
SQL RAG combines:
Natural Language
+
Query Understanding
+
SQL Generation
+
Database Execution
+
Result Validation
+
LLM Generation
to allow an LLM-powered application to retrieve precise information from structured databases.
๐ฏ Learning Objectives¶
After completing this chapter, you will be able to:
- Understand SQL RAG
- Understand why SQL databases complement vector RAG
- Understand Text-to-SQL
- Understand NL2SQL pipelines
- Understand database schema discovery
- Understand schema-aware prompting
- Generate SQL safely
- Validate generated SQL
- Execute read-only SQL queries
- Handle joins and aggregations
- Handle multi-table reasoning
- Combine SQL retrieval with vector retrieval
- Build hybrid SQL + Vector RAG systems
- Understand semantic layers
- Handle enterprise database metadata
- Apply SQL security controls
- Implement query authorization
- Prevent SQL injection through generated queries
- Control query cost and execution time
- Validate SQL results
- Handle ambiguous questions
- Evaluate Text-to-SQL systems
- Design production SQL RAG architectures
- Implement observability and governance
๐ง 1. What Is SQL RAG?¶
SQL RAG is a RAG architecture where the retrieval layer uses structured databases.
Instead of:
SQL RAG uses:
User Query
โ
Query Understanding
โ
Schema Retrieval
โ
SQL Generation
โ
SQL Validation
โ
Database Execution
โ
Structured Result
โ
LLM
โ
Response
๐ 2. Traditional RAG vs SQL RAG¶
Traditional Vector RAG¶
SQL RAG¶
Question
โ
Schema Understanding
โ
SQL Generation
โ
Database Query
โ
Rows / Aggregations
โ
LLM
Hybrid RAG¶
User Query
โ
โโโโโโโโโโดโโโโโโโโโ
โผ โผ
Vector Retrieval SQL Retrieval
โ โ
โผ โผ
Documents Structured Data
โ โ
โโโโโโโโโโฌโโโโโโโโโ
โผ
Evidence Fusion
โ
โผ
LLM
๐ 3. Structured vs Unstructured Knowledge¶
Enterprise AI applications usually need both.
| Data Type | Example | Typical Retrieval |
|---|---|---|
| Unstructured | Policy PDF | Vector Search |
| Unstructured | Architecture Document | Vector Search |
| Semi-structured | JSON | Metadata / Search |
| Structured | Customer Table | SQL |
| Structured | Transactions | SQL |
| Structured | Product Catalog | SQL |
| Relationship-heavy | Dependencies | Knowledge Graph |
| Mixed | Customer + Policy | SQL + Vector |
This leads to an important architectural principle:
๐งฉ 4. What Is Text-to-SQL?¶
Text-to-SQL converts natural-language questions into SQL.
Example:
Generated SQL:
SELECT COUNT(*)
FROM customers
WHERE registration_date >= '2026-01-01'
AND registration_date < '2027-01-01';
The database executes the SQL.
The LLM then converts the result into a user-friendly answer.
๐ 5. Basic Text-to-SQL Pipeline¶
flowchart LR
A["Natural Language Query"] --> B["Schema Retrieval"]
B --> C["SQL Generation"]
C --> D["SQL Validation"]
D --> E["Database"]
E --> F["Query Result"]
F --> G["Answer Generation"]
G --> H["Response"]
๐ง 6. Why SQL RAG?¶
LLMs are excellent at:
Databases are excellent at:
SQL RAG combines these strengths:
๐ 7. Example¶
Question:
The LLM identifies:
Possible SQL:
SELECT SUM(amount) AS total_revenue
FROM transactions
WHERE product = 'Payments'
AND transaction_date >= '2026-07-01'
AND transaction_date < '2026-08-01';
Database:
LLM:
๐๏ธ 8. Production SQL RAG Architecture¶
flowchart TD
A["User"] --> B["API"]
B --> C["RAG Orchestrator"]
C --> D["Intent Detection"]
D --> E["Query Planner"]
E --> F["Schema Retriever"]
F --> G["Relevant Schema"]
G --> H["SQL Generator"]
H --> I["SQL Validator"]
I --> J{"Valid?"}
J -->|No| K["SQL Repair"]
K --> I
J -->|Yes| L["Read-Only Database"]
L --> M["Query Result"]
M --> N["Result Validator"]
N --> O["Answer Generator"]
O --> P["Response"]
๐งฉ 9. Database Schema¶
The LLM cannot reliably generate SQL without understanding the database schema.
Example:
customers
---------
id
name
email
registration_date
country
orders
------
id
customer_id
order_date
total_amount
status
products
--------
id
name
category
price
The schema provides:
๐ 10. Schema Retrieval¶
For a large enterprise database, sending the complete schema to the LLM is inefficient.
Instead:
User Query
โ
Schema Retrieval
โ
Relevant Tables
โ
Relevant Columns
โ
Relevant Relationships
โ
SQL Generation
๐ 11. Schema-Aware Retrieval¶
Question:
Relevant schema might be:
customers
---------
id
name
orders
------
customer_id
total_amount
product_id
products
--------
id
name
Instead of providing hundreds of unrelated tables.
๐ง 12. Schema as Retrieval Context¶
The prompt can contain:
Relevant Tables:
customers
- id
- name
orders
- customer_id
- total_amount
- product_id
products
- id
- name
Relationships:
orders.customer_id โ customers.id
orders.product_id โ products.id
Then ask the model to generate SQL.
๐ท๏ธ 13. Database Metadata¶
Useful metadata includes:
Table Name
Column Name
Data Type
Description
Primary Key
Foreign Key
Relationships
Business Meaning
Sample Values
Sensitivity
Owner
Example:
{
"table": "transactions",
"column": "amount",
"type": "DECIMAL",
"description": "Transaction monetary amount",
"sensitive": false
}
๐ง 14. Business Metadata¶
Technical schema alone may not be sufficient.
For example:
could mean:
Business metadata helps the LLM understand the semantic meaning.
๐งฉ 15. Semantic Layer¶
A semantic layer maps technical database structures to business concepts.
Example:
Another:
This reduces ambiguity in SQL generation.
๐ข 16. Enterprise Semantic Layer¶
flowchart TD
A["Business Question"] --> B["Semantic Layer"]
B --> C["Business Concepts"]
C --> D["Physical Tables"]
D --> E["Columns"]
E --> F["SQL Generator"]
F --> G["Database"]
This creates a separation between:
and:
๐ 17. Schema Relationships¶
SQL generation often requires joins.
Example:
The model needs to understand:
๐ 18. Join Reasoning¶
Question:
Requires:
SELECT DISTINCT c.name
FROM customers c
JOIN orders o
ON o.customer_id = c.id
JOIN products p
ON p.id = o.product_id
WHERE p.name = 'Payments';
This is a multi-table reasoning problem.
๐ง 19. SQL Generation Prompt¶
A controlled prompt might look like:
You are an SQL generation system.
Generate a read-only SQL query.
Rules:
- Use only provided tables and columns.
- Do not modify data.
- Do not access unauthorized tables.
- Do not use SELECT * unless required.
- Return SQL only.
Schema:
customers(
id,
name,
registration_date
)
orders(
id,
customer_id,
order_date,
total_amount
)
Question:
"How much did each customer spend?"
Expected:
SELECT
c.id,
c.name,
SUM(o.total_amount) AS total_spend
FROM customers c
JOIN orders o
ON o.customer_id = c.id
GROUP BY c.id, c.name;
๐ก๏ธ 20. SQL Generation Must Be Constrained¶
Never treat generated SQL as automatically trusted.
The architecture should be:
Not:
๐จ 21. Dangerous SQL¶
A model might generate:
or:
or:
A production SQL RAG system should prevent these operations.
๐ 22. Read-Only Database Access¶
Prefer a dedicated read-only database user.
Conceptually:
RAG Application
โ
โผ
Read-Only DB User
โ
โโโ SELECT โ
โโโ INSERT โ
โโโ UPDATE โ
โโโ DELETE โ
โโโ DROP โ
โโโ ALTER โ
Defense should exist at the database permission layer, not only in prompts.
๐งฉ 23. SQL Validation¶
SQL validation should check:
Syntax
+
Statement Type
+
Allowed Tables
+
Allowed Columns
+
Authorization
+
Query Complexity
+
Resource Limits
Example:
def validate_sql(sql):
parsed = parse_sql(sql)
assert is_select_statement(parsed)
assert only_allowed_tables(parsed)
assert only_allowed_columns(parsed)
assert no_write_operations(parsed)
return True
This is illustrative architecture rather than a complete SQL security implementation.
๐ง 24. SQL AST Validation¶
A stronger approach parses SQL into an Abstract Syntax Tree.
The validator can inspect:
๐ 25. Allowlist Approach¶
Instead of trying to block every dangerous operation:
and explicitly control:
Example:
This is safer than relying solely on blacklist rules.
๐งฉ 26. Row-Level Security¶
Enterprise databases may contain:
A user from Tenant A should not retrieve:
Use database-level or trusted application-level controls such as:
๐ข 27. Multi-Tenant SQL RAG¶
flowchart TD
A["User"] --> B["Identity"]
B --> C["Tenant Context"]
C --> D["Authorization"]
D --> E["Schema Filtering"]
E --> F["SQL Generation"]
F --> G["SQL Validation"]
G --> H["Tenant-Aware Database"]
H --> I["Result"]
The tenant boundary should be enforced outside the LLM.
๐ก๏ธ 28. Sensitive Data¶
Enterprise databases may contain:
The SQL RAG layer should know which columns are sensitive.
Example:
may require additional authorization.
๐ 29. Column-Level Access¶
A user may be allowed:
but not:
The schema retriever should therefore expose only authorized schema information.
๐ง 30. Authorized Schema Retrieval¶
User
โ
Identity
โ
Permissions
โ
Authorized Schema
โ
SQL Generation
โ
SQL Validation
โ
Database
Do not expose unauthorized columns to the model unnecessarily.
๐ 31. Aggregations¶
SQL is particularly powerful for aggregation.
Examples:
Question:
Possible SQL:
This is a strong SQL RAG use case.
๐ 32. Time-Based Queries¶
Natural language dates can be ambiguous.
Example:
The query planner should resolve temporal semantics explicitly.
Example:
The resolved date range should be visible in the generated query or execution trace.
โ ๏ธ 33. Ambiguous Questions¶
Question:
Potential meanings:
A production system should ask for clarification when the semantic layer cannot resolve the ambiguity safely.
๐ง 34. Query Planning¶
Before generating SQL, a planner can extract:
Example:
Question:
"Top 10 products by revenue in July"
Metric:
Revenue
Dimension:
Product
Time:
July
Aggregation:
SUM
Sort:
DESC
Limit:
10
๐ 35. Query Planning Pipeline¶
flowchart LR
A["Natural Language"] --> B["Intent"]
B --> C["Entities"]
C --> D["Metrics"]
D --> E["Filters"]
E --> F["Time Range"]
F --> G["Aggregation"]
G --> H["SQL"]
๐งฉ 36. SQL Generation with Intermediate Representation¶
Instead of going directly:
use:
Example:
{
"metric": "revenue",
"dimensions": ["product"],
"filters": {
"month": "2026-07"
},
"sort": {
"field": "revenue",
"direction": "desc"
},
"limit": 10
}
This intermediate representation can be validated before SQL generation.
๐๏ธ 37. Safer SQL RAG Pipeline¶
User Query
โ
Intent
โ
Query Plan
โ
Authorization
โ
Schema Retrieval
โ
SQL Generation
โ
SQL Parsing
โ
SQL Policy Validation
โ
Cost Validation
โ
Database Execution
โ
Result Validation
โ
Answer Generation
๐ 38. SQL Self-Correction¶
SQL generation can fail.
Example:
but the actual table is:
A controlled repair loop can be:
โ ๏ธ 39. SQL Repair Must Be Bounded¶
Do not allow:
Use:
Example:
๐ง 40. Query Result Validation¶
A successful SQL execution does not guarantee a correct answer.
Example:
may execute successfully but answer the wrong business question.
Therefore validate:
๐ 41. Result Shape Validation¶
Question:
Expected result:
If the query returns:
something may be wrong.
๐ง 42. Semantic Result Validation¶
The system can verify:
For example:
The query may be syntactically valid but semantically incorrect.
๐งฉ 43. Null Handling¶
Generated SQL should consider:
For example:
may be appropriate in some business contexts.
However, business semantics should determine whether:
and:
are equivalent.
๐ 44. Empty Results¶
An empty result does not always mean:
It could mean:
A production system should distinguish:
from:
๐ 45. SQL + Vector RAG¶
Many enterprise questions require both:
Example:
SQL:
Vector:
๐ 46. Hybrid SQL + Vector Architecture¶
flowchart TD
A["User Query"] --> B["Query Planner"]
B --> C["SQL Retrieval"]
B --> D["Vector Retrieval"]
C --> E["Structured Results"]
D --> F["Document Evidence"]
E --> G["Evidence Fusion"]
F --> G
G --> H["LLM"]
H --> I["Response"]
๐ง 47. SQL + Knowledge Graph + Vector¶
A mature enterprise system may use all three:
Example:
Question:
"Which customers were affected by the payment
incident, which services were involved, and what
was the root cause?"
Possible routing:
๐ข 48. Enterprise Knowledge Retrieval¶
flowchart TD
A["User Query"] --> B["Query Understanding"]
B --> C["Query Router"]
C --> D["SQL Retriever"]
C --> E["Graph Retriever"]
C --> F["Vector Retriever"]
D --> G["Structured Evidence"]
E --> H["Relationship Evidence"]
F --> I["Document Evidence"]
G --> J["Evidence Fusion"]
H --> J
I --> J
J --> K["Context Engineering"]
K --> L["LLM"]
L --> M["Validation"]
M --> N["Citation"]
N --> O["Enterprise Response"]
๐งฉ 49. SQL RAG with Semantic Layer¶
The semantic layer can define:
Each business metric maps to:
Example:
Business Metric:
Active Customer
Definition:
Customer with at least one successful transaction
within the active period.
The SQL generator can then use the governed definition.
๐ง 50. Governed Metrics¶
Without a semantic layer:
could be interpreted differently by different queries.
With a governed metric:
The definition becomes consistent.
๐ข 51. Metric Layer¶
This can dramatically improve consistency in enterprise analytics.
๐ง 52. Schema RAG¶
Schema itself can be treated as retrieval data.
Store metadata such as:
Table descriptions
Column descriptions
Relationship descriptions
Business terms
Metric definitions
Example queries
Then:
This is often called a schema-aware or metadata-aware Text-to-SQL architecture.
๐ 53. Few-Shot SQL Examples¶
Example pairs can improve SQL generation.
Question:
"How many customers are active?"
SQL:
SELECT COUNT(*)
FROM customers
WHERE status = 'ACTIVE';
Another:
The system can retrieve examples relevant to the current query.
๐ 54. Example Retrieval¶
flowchart LR
A["User Question"] --> B["Example Retriever"]
B --> C["Relevant SQL Examples"]
C --> D["Schema Context"]
D --> E["SQL Generator"]
E --> F["Validated SQL"]
This can reduce repeated prompt design and improve consistency when examples are well curated.
๐ง 55. SQL RAG Prompt Assembly¶
A production prompt may contain:
SYSTEM RULES
+
AUTHORIZED SCHEMA
+
BUSINESS DEFINITIONS
+
RELEVANT SQL EXAMPLES
+
USER QUESTION
+
QUERY CONSTRAINTS
Example:
SYSTEM:
Generate read-only SQL.
AUTHORIZED TABLES:
customers
orders
products
BUSINESS DEFINITION:
Revenue = successful order total.
EXAMPLE:
...
QUESTION:
What were the top 10 products by revenue last month?
๐ 56. SQL Injection Considerations¶
There are two different concerns:
Traditional SQL Injection¶
User-controlled strings are inserted into SQL unsafely.
LLM-Generated SQL Risk¶
The LLM itself generates an unsafe or unauthorized query.
Both require defense.
Use:
Parameterized Queries
+
Read-Only Credentials
+
SQL Parsing
+
Allowlisting
+
Authorization
+
Database Policies
๐ก๏ธ 57. Prompt Injection¶
User input may contain:
The system should not allow the model to bypass:
Security must be enforced outside the model.
๐จ 58. Database Resource Exhaustion¶
A generated query may be technically valid but expensive.
Example:
This could create an enormous intermediate result.
Therefore enforce:
where supported by the database platform.
โก 59. Query Cost Control¶
A production pipeline can use:
Conceptually:
The exact cost mechanism depends on the database engine.
๐ 60. LIMIT and Pagination¶
For exploratory queries:
may protect the system.
However, blindly adding:
can produce incorrect answers for aggregation queries.
For example:
would not mean:
Therefore limits must be applied according to query semantics.
๐ง 61. Exactness Matters¶
SQL is particularly useful because databases provide deterministic computation.
For:
the database should perform the computation.
Do not ask the LLM to calculate:
from retrieved text.
Use:
๐งฉ 62. LLM Should Not Become the Database¶
Bad architecture:
Better:
This improves:
๐ 63. Result-to-Text Generation¶
Database result:
The LLM can generate:
Payments generated the highest revenue
at โน12.45 million, followed by Loans at
โน9.8 million and Cards at โน7.4 million.
The LLM is primarily performing:
rather than database computation.
๐ง 64. SQL RAG Response Contract¶
A useful internal response model might contain:
{
"question": "...",
"sql": "...",
"columns": [],
"rows": [],
"execution_time_ms": 42,
"source": "analytics-db",
"schema_version": "v12"
}
The final response generator can consume this structured result.
๐ 65. Citation for SQL RAG¶
SQL answers need a different type of attribution from document RAG.
Instead of:
the system may expose:
For sensitive systems, expose only the level of SQL/database detail appropriate for the user.
๐งพ 66. Data Provenance¶
SQL RAG should preserve:
This enables auditing.
๐ง 67. SQL Query Logging¶
A production trace may include:
Trace ID
User
Tenant
Question
Schema Retrieved
Generated SQL
Validation Result
Execution Time
Rows Returned
Database
Error
Final Response
Sensitive values should be redacted from logs where appropriate.
๐ 68. SQL RAG Observability¶
Track:
SQL Generation Latency
Schema Retrieval Latency
SQL Validation Latency
Database Execution Latency
Total Latency
SQL Error Rate
Repair Rate
Empty Result Rate
Query Cost
Rows Returned
๐ 69. SQL RAG Dashboard¶
| Metric | Purpose |
|---|---|
| SQL Success Rate | Query reliability |
| SQL Repair Rate | Generation quality |
| Query Latency | Performance |
| DB Latency | Database performance |
| Empty Result Rate | Query quality |
| Unauthorized Query Rate | Security |
| Query Cost | Resource usage |
| Rows Returned | Result size |
| Schema Retrieval Accuracy | Context quality |
| Answer Accuracy | End-to-end quality |
๐งช 70. Text-to-SQL Evaluation¶
Evaluation should measure more than whether SQL executes.
Possible metrics:
SQL Syntax Accuracy
SQL Execution Accuracy
Exact Match
Component Match
Result Accuracy
Answer Accuracy
๐ 71. Execution Accuracy¶
Suppose two SQL queries are structurally different:
and:
Both may return the correct answer.
Therefore execution/result correctness can be more useful than literal SQL string comparison.
๐ง 72. Query Result Accuracy¶
The ultimate question is:
Evaluation dataset:
The generated SQL can be executed against a controlled evaluation database.
๐งช 73. SQL Evaluation Dataset¶
A strong dataset should include:
Simple Queries
Filters
Joins
Aggregations
Nested Queries
Time-Based Queries
Ambiguous Questions
Multi-Table Questions
Business Metrics
Security Cases
๐จ 74. Security Evaluation¶
Test cases should include:
Unauthorized Table
Unauthorized Column
Write Operation
Cross-Tenant Query
Sensitive Column Access
Expensive Query
Prompt Injection
Schema Injection
Example:
Expected:
๐ง 75. SQL RAG Failure Modes¶
Common failures:
Wrong Table
Wrong Column
Wrong Join
Wrong Filter
Wrong Aggregation
Wrong Date Range
Wrong Metric Definition
Unauthorized Access
Expensive Query
Empty Result
Incorrect Result Interpretation
๐จ 76. Wrong Join¶
Suppose:
is joined incorrectly to:
The query may execute successfully but return incorrect results.
This is a semantic failure, not a syntax failure.
๐ 77. Date Errors¶
Question:
Potential interpretations:
The system should resolve the time semantics explicitly.
๐ง 78. Metric Definition Errors¶
Question:
Possible definitions:
A governed semantic layer can reduce these ambiguities.
๐ข 79. Enterprise SQL RAG Architecture¶
USER
โ
โผ
Query Understanding
โ
โผ
Query Planner
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
Schema Semantic Example
Retrieval Layer Retrieval
โ โ โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โผ
SQL Generation
โ
โผ
SQL Validation
โ
โโโโโโโโดโโโโโโโ
โ โ
Valid Invalid
โ โ
โผ โผ
Cost Analysis Repair
โ โ
โผ โ
Authorization โ
โ โ
โโโโโโโโฌโโโโโโโ
โผ
Read-Only DB
โ
โผ
SQL Result
โ
โผ
Result Validation
โ
โผ
Answer Generator
โ
โผ
Response
๐ 80. Full Multi-Source Enterprise RAG¶
flowchart TD
A["User Query"] --> B["Query Planner"]
B --> C["Vector Search"]
B --> D["Knowledge Graph"]
B --> E["SQL Database"]
C --> F["Document Evidence"]
D --> G["Relationship Evidence"]
E --> H["Structured Evidence"]
F --> I["Evidence Fusion"]
G --> I
H --> I
I --> J["Re-ranking"]
J --> K["Context Engineering"]
K --> L["LLM"]
L --> M["Response Validation"]
M --> N["Citation / Attribution"]
N --> O["Enterprise Response"]
๐ง 81. When to Use SQL RAG¶
Use SQL RAG when questions require:
Exact Counts
Aggregations
Filtering
Sorting
Joins
Transactions
Time-Series Analysis
Structured Business Metrics
Examples:
How many customers?
What is total revenue?
Which product sold the most?
Which customers placed more than 10 orders?
What were failed payments yesterday?
๐ซ 82. When Not to Use SQL RAG¶
Do not force SQL when the information is primarily in:
For:
vector retrieval is usually more appropriate.
๐ 83. Decision Framework¶
Question
โ
โผ
What type of knowledge?
โ
โโโ Unstructured โ Vector
โ
โโโ Structured โ SQL
โ
โโโ Relationship-heavy โ Graph
โ
โโโ Mixed โ Hybrid
This simple routing model becomes increasingly useful as enterprise RAG systems grow.
๐งฉ 84. SQL RAG + Graph RAG¶
Some questions require both.
Example:
Possible flow:
Then combine the results.
๐๏ธ 85. SQL + Graph Architecture¶
flowchart LR
A["Query"] --> B["Planner"]
B --> C["SQL"]
B --> D["Graph"]
C --> E["Customers / Products"]
D --> F["Dependencies / Hosting"]
E --> G["Fusion"]
F --> G
G --> H["LLM"]
๐ค 86. SQL RAG + Agentic RAG¶
An agent can use SQL as a tool:
Agent
โ
โโโ SQL Tool
โโโ Vector Search
โโโ Knowledge Graph
โโโ Other Tools
Example:
Agent
โ
"What is the current transaction volume?"
โ
SQL Tool
โ
Result
โ
"Why did it change?"
โ
Vector Search
โ
Incident Documentation
This allows iterative retrieval.
๐ง 87. SQL Tool Interface¶
A controlled tool might expose:
Notice that the interface can accept:
instead of arbitrary SQL from the agent.
This provides another policy boundary.
๐ก๏ธ 88. Query Plan as a Security Boundary¶
Instead of:
prefer:
This can reduce the attack surface.
๐ง 89. Query Plan Example¶
{
"table": "transactions",
"metrics": [
{
"name": "count"
}
],
"filters": [
{
"field": "status",
"operator": "=",
"value": "FAILED"
}
],
"time_range": {
"from": "2026-08-10",
"to": "2026-08-11"
}
}
The SQL generator can convert this controlled representation into SQL.
โก 90. Performance Optimization¶
Key optimization areas:
Schema Retrieval
SQL Generation
SQL Validation
Database Execution
Result Serialization
LLM Generation
Database-side optimization remains especially important.
๐๏ธ 91. Database Optimization¶
Generated SQL should leverage:
The RAG layer should not attempt to replace database engineering.
๐ 92. Minimize Data Returned¶
Avoid:
when unnecessary.
Prefer:
This reduces:
๐ง 93. Push Computation to Database¶
Prefer:
over:
The database should perform structured computation.
๐ 94. Query Result Compression¶
For large results:
Do not pass unnecessary raw data into the model.
๐งฉ 95. Result Summarization¶
Example database output:
Country | Transactions | Revenue
--------|--------------|---------
India | 120000 | 4.2M
Germany | 90000 | 3.8M
UK | 70000 | 2.9M
The LLM can summarize:
๐งช 96. Practical Exercise¶
Create a sample database:
Example:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(200),
country VARCHAR(100)
);
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name VARCHAR(200),
category VARCHAR(100)
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
product_id INTEGER,
order_date DATE,
amount DECIMAL(18,2)
);
๐ 97. Practice Questions¶
Implement:
1. Count customers.
2. Count customers by country.
3. Calculate total revenue.
4. Calculate revenue by product.
5. Find top 10 customers by spending.
6. Find the most popular product.
7. Find customers with more than 10 orders.
8. Find revenue for the previous month.
9. Find products with no orders.
10. Find average order value.
๐งช 98. Add Natural Language¶
Convert:
into:
Then:
into:
SELECT
country,
COUNT(*) AS customer_count
FROM customers
GROUP BY country
ORDER BY customer_count DESC
LIMIT 1;
๐ง 99. Add Schema Retrieval¶
For a larger schema:
Experiment with:
Observe how schema retrieval affects SQL generation.
๐งช 100. Add Security¶
Test:
Expected:
Test:
Expected:
Test:
when the user belongs to Tenant A.
Expected:
๐ 101. Evaluate the System¶
Measure:
Compare:
๐จ 102. Common Mistakes¶
Mistake 1 โ Giving the Entire Database Schema to the LLM¶
Large schemas create:
Prefer schema retrieval.
Mistake 2 โ Trusting Generated SQL¶
Generated SQL must be validated.
Mistake 3 โ Allowing Write Access¶
Use read-only credentials.
Mistake 4 โ Ignoring Business Definitions¶
Technical column names are not always sufficient.
Mistake 5 โ Asking the LLM to Perform Large Calculations¶
Use the database.
Mistake 6 โ Ignoring Query Cost¶
A valid SQL query can still be operationally dangerous.
Mistake 7 โ Ignoring Multi-Tenancy¶
Tenant isolation must be enforced independently of the model.
Mistake 8 โ Treating Empty Results as Truth¶
Investigate whether the query itself is wrong.
๐ 103. Production Checklist¶
โ Identify SQL RAG use cases
โ Identify structured data sources
โ Identify database owners
โ Document database schemas
โ Document relationships
โ Document business definitions
โ Build schema metadata
โ Build schema retrieval
โ Build semantic layer
โ Define governed metrics
โ Curate SQL examples
โ Implement query planning
โ Implement SQL generation
โ Implement SQL parsing
โ Implement SQL validation
โ Implement allowlists
โ Implement query cost controls
โ Use read-only database credentials
โ Implement authentication
โ Implement authorization
โ Implement tenant isolation
โ Implement column-level security
โ Protect sensitive data
โ Implement bounded retries
โ Implement SQL repair
โ Implement result validation
โ Handle empty results
โ Handle ambiguous questions
โ Optimize database queries
โ Apply appropriate indexes
โ Limit result size
โ Push computation to database
โ Avoid SELECT * where unnecessary
โ Integrate Vector RAG
โ Integrate Knowledge Graph
โ Implement hybrid routing
โ Implement evidence fusion
โ Preserve query provenance
โ Log database source
โ Log schema version
โ Log query execution metadata
โ Measure SQL accuracy
โ Measure execution accuracy
โ Measure result accuracy
โ Measure answer accuracy
โ Measure security violations
โ Monitor latency
โ Monitor query cost
โ Monitor error rate
โ Monitor repair rate
โ Monitor empty-result rate
โ Build regression dataset
โ Test ambiguous queries
โ Test security scenarios
โ Test tenant isolation
โ Load test database access
๐ 104. Key Takeaways¶
- SQL RAG connects natural-language questions to structured enterprise databases.
- Text-to-SQL is a core component of SQL RAG.
- Databases are better than LLMs at exact structured computation.
- SQL is particularly useful for filtering, joins, aggregation, sorting, and counting.
- Schema understanding is critical for reliable SQL generation.
- Large enterprise schemas should be retrieved selectively rather than blindly passed to the LLM.
- Business metadata is often as important as technical schema metadata.
- A semantic layer can map business concepts to physical database structures.
- Governed metric definitions can reduce inconsistent interpretations of business terms.
- Query planning can provide an intermediate representation between natural language and SQL.
- SQL should be parsed and validated before execution.
- Read-only credentials should be used for RAG database access.
- Database permissions should provide a security boundary independent of the LLM.
- Allowlisting authorized tables and columns is safer than relying only on prompt instructions.
- Tenant isolation must be enforced outside the model.
- Sensitive columns require additional authorization controls.
- SQL injection and LLM-generated unsafe SQL are related but distinct security concerns.
- Query cost controls are necessary because syntactically valid queries can still be operationally expensive.
- Result validation is necessary because successful SQL execution does not guarantee semantic correctness.
- Empty results should be investigated rather than automatically treated as authoritative.
- SQL repair loops should be bounded.
- Database computation should remain inside the database whenever possible.
- SQL RAG and Vector RAG complement each other.
- SQL can provide exact structured facts while vector retrieval provides unstructured evidence.
- Knowledge Graphs can provide relationship-based retrieval.
- A mature enterprise RAG system may combine SQL, Graph, Vector, and other retrieval mechanisms.
- Agents can use SQL through controlled tools rather than arbitrary database access.
- Production SQL RAG requires evaluation, observability, security, governance, and cost controls.
๐ง Final Mental Model¶
USER QUESTION
โ
โผ
QUERY UNDERSTANDING
โ
โผ
QUERY PLANNER
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โผ โผ โผ
Intent Metrics Filters
โ โ โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โผ
AUTHORIZATION
โ
โผ
SCHEMA RETRIEVAL
โ
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโ
โผ โผ โผ
Tables Columns Relationships
โ โ โ
โโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโ
โผ
SEMANTIC LAYER
โ
โผ
SQL GENERATION
โ
โผ
SQL PARSING
โ
โผ
POLICY VALIDATION
โ
โโโโโโโดโโโโโโ
โผ โผ
Valid Invalid
โ โ
โผ โผ
COST CHECK REPAIR
โ โ
โผ โ
READ-ONLY DB โโโโโโ
โ
โผ
SQL EXECUTION
โ
โผ
RESULT VALIDATION
โ
โผ
STRUCTURED DATA
โ
โโโโโโโโโโโโโผโโโโโโโโโโโโ
โผ โผ โผ
SQL Vector Graph
Evidence Evidence Evidence
โ โ โ
โโโโโโโโโโโโโผโโโโโโโโโโโโ
โผ
EVIDENCE FUSION
โ
โผ
CONTEXT ENGINEERING
โ
โผ
LLM
โ
โโโโโโโโโดโโโโโโโโ
โผ โผ
Validation Citation
โ โ
โโโโโโโโโฌโโโโโโโโ
โผ
ENTERPRISE RESPONSE
The core principle is:
SQL RAG does not turn the LLM into a database. It gives the LLM a controlled mechanism for asking the database precise questions, while the database remains responsible for authoritative structured computation.
The strongest enterprise architecture is therefore:
Natural Language
โ
Query Planning
โ
Schema + Semantic Retrieval
โ
Controlled SQL Generation
โ
Validation + Authorization
โ
Database
โ
Exact Structured Result
โ
Evidence Fusion
โ
LLM
โ
Validated Enterprise Response
And when the question spans multiple knowledge types:
Enterprise Question
โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโ
โผ โผ โผ
SQL Graph Vector
โ โ โ
โผ โผ โผ
Facts Relationships Documents
โ โ โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโ
โผ
Evidence Fusion
โ
โผ
LLM
This makes SQL RAG an important component of a broader enterprise knowledge retrieval architecture, rather than a replacement for Vector RAG or Graph RAG.
๐งญ Chapter Navigation¶
Part V โ Advanced Retrieval-Augmented Generation¶
Previous:
03. Knowledge Graphs for RAG
Next:
05. Multimodal RAG
Section:
05 โ Advanced RAG Architecture
Advanced RAG Architecture Path¶
01 Advanced RAG Architecture
โ
02 Graph RAG
โ
03 Knowledge Graphs for RAG
โ
04 SQL RAG
โ
05 Multimodal RAG
โ
06 Agentic RAG
Enterprise AI Engineering Handbook
Building Production-Grade Enterprise AI Systems โ One Chapter at a Time.