RAG
Text-to-SQL
Let the model write queries against your database — read-only, schema-aware and checked.
What it is and why it exists
The model gets the relevant schema (tables, columns, descriptions, example values), writes SQL, the system validates and executes it under a read-only role with limits, and the model explains the result. Accuracy depends mostly on schema context and a semantic layer of business definitions.
Quick prototype
Dump the whole schema in the prompt and run what comes back.
In production, at scale
Retrieve only relevant tables, provide a semantic layer (what “active customer” means), validate SQL (parse, allow-list tables, add LIMIT), execute with a read-only least-privilege role and timeouts, show the SQL to the user, and evaluate on question→result pairs.
Vendors and tools
| Vanna / LangChain SQL agents | Text-to-SQL frameworks |
|---|---|
| dbt Semantic Layer / Cube | Business definitions as code |
| Snowflake Cortex Analyst / Databricks Genie | Warehouse-native NL-to-SQL |
On AWS: Amazon Q in QuickSight; Bedrock Knowledge Bases structured data retrieval (natural language to SQL over Redshift).
How it fails
- A write or a huge cross join on a production database
- Plausible SQL with the wrong business definition
- Exposing columns the user may not see
What the interviewer asks next
How do you make text-to-SQL safe?
Read-only role scoped to allowed views, statement allow-list (SELECT only), row limits and timeouts, row-level security for tenants, and showing the query to the user.