GuideIntermediate

Sql For Llms And Agents

Learn the engineering behind having an LLM or agent generate and execute SQL safely and reliably against your database: text-to-SQL. This guide covers how to give the model the schema as context (serializing `CREATE TABLE`, sample rows, relationships, and the token budget), how to prompt for correct SQL (few-shot, fixing the dialect, constraint instructions), how to validate and execute generated SQL before trusting it (is it a `SELECT`? does it reference real tables?), security guardrails (read-only, blocking destructive statements, row limits, anti-injection, least privilege), the SQL agent loop (question → SQL → validate → run → correct → answer, with the `run_sql` tool), and how to evaluate a text-to-SQL assistant with execution accuracy. Everything is built on top of the Reservo database with sqlite3; the LLM call is shown as a realistic example using the Claude API. It closes with a capstone project: a safe, measurable, end-to-end SQL assistant for Reservo.

64
lessons
8
modules
English · Spanish
available in
Yes
certificate
Free
access
NIEVA

Outcomes

What you'll be able to do

  • Understand the text-to-SQL problem: the promise of querying a database by talking to it, and the dangers (incorrect SQL, hallucinated columns, destructive queries)
  • Give an LLM the schema as context: serialize `CREATE TABLE`, sample rows, and relationships while respecting a token budget
  • Prompt for correct SQL: few-shot examples, fixing the SQL dialect, constraint instructions, and handling ambiguity
  • Validate generated SQL before running it: does it parse? is it a `SELECT`? does it reference real tables and columns? `EXPLAIN QUERY PLAN`
  • Implement security guardrails: enforce read-only access, block destructive statements, row limits and timeouts, and the least-privilege principle
  • Build the SQL agent loop: question → generate SQL → validate → run → correct → answer, using the `run_sql` tool
  • Evaluate a text-to-SQL assistant with execution accuracy, comparing against a question-to-gold-SQL test set
  • Build a safe, measurable, end-to-end SQL assistant for Reservo as a capstone project

Before you start

What you need to bring

It's for you if...

  • Devs who want to expose a database to an LLM or agent without risking data deletion or leaks
  • Backend devs who already know SQL and want a language model to generate it reliably
  • Teams evaluating building a natural-language query assistant over their database

Requirements and materials

  • Intermediate/advanced SQL (JOINs, aggregations) — or the Advanced SQL Querying Guide completed
  • Python 3.10+ (uses `sqlite3` from the standard library, no additional install needed)
  • Basic familiarity with the concept of LLM tool-calling/function-calling (explained in the guide)

Content

The syllabus, module by module

Open any of them to see its lessons.

Common questions

What people usually ask

Start whenever you like

Reviews

What students say

These reviews are from enrolled students who completed at least 50% of the course. We moderate reviews only on content grounds (spam, offensive language, personal data), never for being critical or negative.

No approved reviews yet.

Be the first to share your experience!