Generating SQL Queries from Natural Language
Learn how to prompt an LLM to translate plain English questions into correct SQL by supplying schema context, examples, and safety guardrails.
Generating SQL Queries from Natural Language is a free Prompt Engineering & LLM Optimization for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Prompt Engineering & LLM Optimization for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Text-to-SQL Overview
One of the most practical developer uses of LLMs is turning a plain question like How many orders shipped last week? into a runnable SQL query.
Done well, it lets non-experts query data and speeds up everyday analysis.
The Model Needs the Schema
An LLM cannot guess your table and column names. Always include the relevant schema in the prompt so it references real fields.
Schema:
orders(id, customer_id, status, total, created_at)
customers(id, name, country)A Basic Text-to-SQL Prompt
Combine schema, the question, and an instruction to return only SQL.
Given the schema above, write a single
Postgres query. Return only SQL.
Question: total revenue per country last month.Specify the Dialect
SQL dialects differ (Postgres, MySQL, SQLite, BigQuery). State which one you use, or the model may emit functions your database does not support.
Few-Shot Examples Help
Showing one or two question-to-SQL pairs teaches the model your conventions, like how you format dates or name aliases.
Q: customers from Japan
SQL: SELECT * FROM customers WHERE country = 'Japan';
Q: orders over 100
SQL: SELECT * FROM orders WHERE total > 100;Handling Joins
For questions spanning tables, hint at the relationships in the schema (foreign keys) so the model joins correctly instead of guessing.
-- orders.customer_id references customers.id
SELECT c.country, SUM(o.total)
FROM orders o JOIN customers c
ON o.customer_id = c.id
GROUP BY c.country;Guardrail: Read-Only
Never run model-generated SQL with write access. Restrict the connection to SELECT only and reject any query containing DELETE, UPDATE, or DROP.
if (/\b(delete|update|drop|insert)\b/i.test(sql)) {
throw new Error('Only read queries allowed');
}Validate Before Executing
Parse or EXPLAIN the query before running it. This catches syntax errors and lets you reject queries touching tables outside an allowlist.
EXPLAIN SELECT ...; -- check plan, no data fetchedSelf-Correction Loop
If the query errors, feed the database error message back to the model and ask it to fix the SQL. Two or three attempts usually resolve most mistakes.
The query failed with: column "revenue"
does not exist. Fix the SQL.Explaining Results
After running the query, you can ask the model to summarize the rows in plain language, closing the loop from question to readable answer.
Limits to Remember
- Ambiguous questions yield ambiguous SQL.
- Large schemas may not fit in context, retrieve the relevant tables.
- Always verify aggregates on critical reports.
Quick Check
Test your understanding of text-to-SQL.
Recap
Text-to-SQL works by giving the model the schema, dialect, and few-shot examples, then validating output. Always run generated SQL read-only, validate before executing, and use a self-correction loop on errors.
Frequently asked questions
Is the “Generating SQL Queries from Natural Language” lesson free?
Yes — the full text of “Generating SQL Queries from Natural Language” is free to read here on the web, and the Prompt Engineering & LLM Optimization for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Prompt Engineering & LLM Optimization for Developers course, upgrade to CoddyKit PRO.
What will I learn in “Generating SQL Queries from Natural Language”?
Learn how to prompt an LLM to translate plain English questions into correct SQL by supplying schema context, examples, and safety guardrails. You practise Prompt Engineering & LLM Optimization for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Prompt Engineering & LLM Optimization for Developers?
No prior experience is required. Prompt Engineering & LLM Optimization for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generating SQL Queries from Natural Language” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Prompt Engineering & LLM Optimization for Developers lesson?
Yes. Every Prompt Engineering & LLM Optimization for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Code Generation & Refactoring
- Debugging & Test Case Generation
- Data Extraction & Summarization
- Generating SQL Queries from Natural Language