Turn Plain English Into SQL Queries with Go and LLMs

Ardan Labs
Ardan LabsMar 16, 2026

Why It Matters

It lowers the barrier for organizations to provide natural‑language data access, cutting development time and enabling on‑prem LLM deployments that keep sensitive data inside the firewall.

Key Takeaways

  • Use LLMs in Go to translate English to SQL queries.
  • Validate generated SQL to allow only SELECT statements for safety.
  • Convert query results to CSV before feeding back to LLM.
  • Chron library runs LLM models locally without external server.
  • Debug flag reveals intermediate prompts, aiding prompt engineering.

Summary

The video walks through a Go‑based prototype that lets users ask plain‑English questions about a DuckDB database and have a large language model generate the corresponding SQL, execute it, and return a natural‑language answer.

The implementation follows a two‑prompt workflow: the first prompt asks the LLM to produce a SELECT‑only SQL statement given the schema and user query; the resulting query is run against the database, its rows are serialized to CSV, and a second prompt feeds the original question, the SQL, and the CSV results back to the model to craft the final answer. Safety checks strip non‑SELECT commands, and a debug flag can expose intermediate outputs for prompt tuning.

The presenter highlights practical details such as embedding prompt templates with Go embed, using the Chron library to host LLMs locally via the OpenAI API contract, and leveraging LangChain‑go for provider‑agnostic calls. An example interaction—“how many rides in the database?”—demonstrates the system returning roughly 650 000 rides after cleaning unwanted markdown, with the entire codebase under 150 lines.

By abstracting the natural‑language‑to‑SQL translation into a few hundred lines of Go, the demo shows how businesses can rapidly build secure, on‑prem data‑access assistants without extensive custom parsing logic, opening the door to broader self‑service analytics while preserving data privacy.

Original Description

Learn how to build a natural language to SQL system in Go that lets users query a database using plain English with the help of an LLM.
In this video, Miki Tebeka demonstrates how to build an AI powered workflow that converts natural language questions into SQL queries, executes them against a database, and returns human readable answers.
Read the full companion article:
This tutorial shows how developers can build a text to SQL system using Go and modern AI tooling to enable natural language database queries.
What You Will Learn
• How to convert natural language questions into SQL queries
• How to safely execute LLM generated SQL against a database
• How to build a workflow that lets users query databases using plain English
• How to structure prompts for natural language to SQL generation
• How to return structured database results as human readable answers
Workflow Overview
The system operates through a sequence of interactions between the Go application, the LLM, and the database.
1. SQL Generation
The user submits a question. The application sends the question and database schema to the LLM which generates a SQL query.
2. Query Execution
The generated SQL runs against the database. The application verifies that only SELECT statements are executed to prevent unauthorized changes.
3. Data Conversion
Database results are converted into CSV so the LLM can process them as text input.
4. Final Response
The original question, SQL query, and CSV data are sent back to the LLM to produce a natural language answer.
Technical Stack:
Go
LangChain
DuckDB
LLMs using the OpenAI protocol
Prompt management with Go embed
The example database contains bike station and trip data used to demonstrate natural language database queries.

Learn More from Ardan Labs

Connect with Ardan Labs
#golang #ai #llm #sql #softwareengineering

Comments

Want to join the conversation?

Loading comments...