NL2SQL: Natural Language to Safe, Executable SQL¶
Query Databases with English, Not Syntax¶
Analysts need answers fast but schemas are complex. NL2SQL converts natural language into parameterized SQL grounded in your schema, with safety checks and explanations.
About the Solution¶
NL2SQL introspects databases, builds a schema graph, and uses constrained decoding to produce portable SQL (Postgres/MySQL/SQL Server). It returns the query, rationale, and a preview with row/column sampling.
How AI Powers the Solution¶
- Schema-Grounded Parsing: Catalog + examples + column stats for disambiguation.
- Guardrails: Read-only roles, query budget, timeouts, denylist, row-level filters.
- Verification: Dry-run + EXPLAIN + semantic checks before execution.
- Learning: Feedback loop captures accepted/edited queries as few-shot hints.
Business Value¶
- Speed: Ad-hoc answers without waiting on BI tickets.
- Safety: Principle-of-least-privilege and query linting.
- Consistency: Shared definitions and templates.
Workflow¶
- User asks a question.
- Engine maps terms → tables/columns.
- Generates SQL with parameters.
- Verifies, executes, and returns preview + download link.
API Endpoints¶
POST /nl2sql/query
{
"question": "Revenue by month for 2024",
"dialect": "postgres",
"limit": 100
}
Sample Response
{
"sql": "SELECT date_trunc('month', o.created_at) m, sum(o.total) rev ...",
"explain": "Group orders by month excluding refunds...",
"preview": {
"columns": ["m", "rev"],
"rows": [["2024-01-01", 12345.67]]
}
}
Security¶
- Read-only service account, masked PII, audit logs, per-tenant VPC peering.
Example Outputs¶
- SQL + rationale
- CSV download link
- Saved chart spec