The Data Access Problem
Only 3 people on your team can write SQL. Everyone else files a ticket and waits 2 days. NL-SQL changes this — type a question, get data instantly.
How It Works
- User asks: "Top 10 customers by revenue last quarter?"
- LLM receives question + schema, generates SQL
- System executes on a read-only replica
async function nlToSql(question: string, schema: string) {
const res = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: `SQL expert. Schema:\n${schema}\nConvert to SELECT only.` },
{ role: 'user', content: question }
],
temperature: 0,
});
return res.choices[0].message.content;
}
Safety Guardrails
- Read-only — No writes or deletes
- Timeout — Kill after 10 seconds
- Row limit — Cap at 1,000 rows
- Schema filtering — Hide sensitive tables
Accuracy
| Query Type | Accuracy |
|---|
| Simple aggregation | ~95% |
| Multi-table join | ~85% |
| Time-series | ~80% |
| Complex subqueries | ~65% |
Use NL-SQL for ad-hoc exploration, dashboards for recurring metrics.