Databases · DBC-49

Data Agents and Text-to-SQL

Data Agents and Text-to-SQL is a hands-on course in building AI that answers business questions from a database, covering schema context, a semantic layer, read-only roles, SQL guardrails, accuracy evaluation and database access through MCP. It suits data analysts, data engineers and AI teams building self-service data tools, and you leave with a working data agent prototype and a test set to measure it.

Updated
From 6,210 THB / person 6,900 −10% excl. VAT 7% · group rates available
PDFDownload the course outline
  • Duration12 hours · 2 days
  • FormatOnsite / live online
  • Next roundOn request
  • CertificateIncluded

Course overview

Many organisations want staff to ask questions of their databases in plain language instead of waiting for the data team to write SQL every time. Today's LLMs write SQL far better than before, but once they are connected to a real database the same problems keep appearing: the wrong table is chosen, revenue is defined differently from the way finance defines it, numbers look convincing but are wrong, or worse, a query changes data or pulls personal data out by accident.

This course shows how to build a data agent that answers questions from a database accurately and safely. Learners start with text-to-SQL through an LLM API, then improve it with schema context, column descriptions, example queries and a semantic layer that defines metrics clearly. They then add layers of protection with read-only roles, SQL checks before execution, timeouts and masking of personal data, measure accuracy with a test question set, and connect the database to an AI assistant through MCP. The course closes with building a data agent for a business team end to end. (2 days, 6 hours per day, 12 hours in total, Intermediate level.)

What you’ll gain

  • Explain the architecture of text-to-SQL and data agents, and where they usually go wrong
  • Build text-to-SQL with an LLM API and Python against a PostgreSQL database
  • Prepare schema context, column descriptions and example queries that make AI answers more accurate
  • Define metrics and table relationships in a semantic layer
  • Prevent wrong or unsafe queries with read-only roles and multiple layers of guardrails
  • Measure data agent accuracy with a test question set and clear criteria
  • Connect a database to an AI assistant through MCP safely
  • Design a data agent that shows its SQL and explanation so users can check the answer

Who this course is for

  • Data analysts and BI developers who field a constant stream of data questions from business teams
  • Data engineers and backend developers asked to build an AI system for querying data
  • AI and innovation teams running a data agent proof of concept
  • DBAs and data governance teams who must set permissions and safeguards when AI touches a database
  • Anyone already using AI to write SQL who wants to turn it into a system the organisation can rely on

Prerequisites

  • SQL at the level of SELECT, JOIN, GROUP BY and subqueries
  • Basic Python, such as functions, lists, dictionaries and using libraries
  • A Google account for Google Colab and an API key for at least one LLM, such as Claude or Gemini
  • A laptop on which you can install software, for the MCP database lab

Curriculum

Course Details

This course runs for 2 days, 6 hours per day (12 hours in total, 09:00-16:00), as lectures with labs on Google Colab using one sample sales database of a fictional company throughout. Intermediate level. It builds on Claude AI for Data Analyst, which focuses on analysts using AI for their own work; this course instead focuses on building a data question system that others in the organisation can use

accurately and safely. It also differs from PostgreSQL for AI Apps with pgvector, which focuses on vector search. Learners use their own or their company's Google account and LLM API key, and API usage may be charged. The MCP database lab runs on the learner's laptop. Learners take home notebooks for every lab and templates for a data dictionary, a semantic layer and a test question set to use with their own data.

Day 1 From Text-to-SQL to a Data Agent That Understands the Business

Section 1: Data Agents and Text-to-SQL Overview

  • How text-to-SQL, data agents and traditional BI differ, and which jobs each suits
  • The workflow: take the question, pick tables, write SQL, run it, check the result and explain
  • Why AI answers go wrong: ambiguous column names, mismatched metric definitions and bad joins
  • Lab: set up Google Colab, install PostgreSQL and load the sample sales database

Section 2: Lab: Your First Text-to-SQL with an LLM API

  • Call the Claude API or Gemini API from Python and handle API keys safely
  • Design a prompt that sends the schema and question and returns SQL only
  • Run the SQL with psycopg, show the result as a DataFrame and let the LLM summarise it
  • Lab: ask 10 business questions and record which answers are wrong and why

Section 3: Schema Context the AI Needs

  • Pull the schema from information_schema, with primary and foreign keys
  • Describe tables, columns, units and the allowed values of status columns
  • Share a few sample rows carefully without exposing personal data
  • Lab: build a data dictionary and measure results before and after adding context

Section 4: Semantic Layer and Metric Definitions

  • Why revenue, new customer or profit need a single definition across the organisation
  • Define metrics, dimensions and join paths in a YAML file the team maintains together
  • How semantic layer tools such as dbt Semantic Layer or Cube approach the problem
  • Let the AI pick a defined metric instead of inventing a formula each time
  • Lab: build a small semantic layer the agent uses to answer sales questions

Section 5: Few-shot Examples and a Query Library

  • Collect question and SQL pairs reviewed by the data team as examples for the AI
  • Retrieve the closest examples with embeddings instead of sending them all
  • Handle ambiguous questions by having the agent ask back when information is missing
  • Lab: add a query library and compare accuracy with the previous round

Section 6: Lab: A Data Agent for Multi-step Questions

  • Tool calling: let the agent explore the schema, run queries and check results step by step
  • Let the agent fix its SQL when a query errors or returns nothing
  • Break complex questions, such as regional sales against last year, into several queries
  • Lab: build a data agent that answers with the SQL it used and the assumptions it made
Day 2 Safe, Measurable and Ready for Real Use

Section 7: Locking Down Database Access for AI

  • The risks when AI runs SQL: changed data, heavy queries that slow systems and data leaks
  • Create a read-only role and a dedicated schema or views that the AI can see
  • Set statement_timeout, cap the number of rows and point the agent at a read replica
  • Lab: prove the agent cannot delete or change data, even when told to in a prompt

Section 8: Lab: SQL Guardrails Before Execution

  • Parse SQL with sqlglot to confirm it is a SELECT statement only
  • Allowlist tables and columns and add a LIMIT automatically
  • Mask personal data in results and log every question and SQL statement
  • Deal with prompt injection hidden in data or in user questions

Section 9: Evaluation: Measuring Data Agent Accuracy

  • Build a test question set with correct answers from real business team questions
  • Execution accuracy: compare query results rather than the SQL text
  • Classify errors, such as wrong table, wrong filter or wrong metric definition
  • Lab: build an evaluation notebook and report scores before and after improvements

Section 10: Lab: Database Access through MCP

  • What the Model Context Protocol (MCP) is and how it gives AI assistants access to data
  • Choose a database MCP server, such as MCP Toolbox for Databases, and confirm it runs read-only
  • Connect PostgreSQL to Claude Desktop or another MCP client using a read-only role
  • Write your own MCP tool that runs metrics from the semantic layer instead of free-form SQL

Section 11: Delivering Answers Users Can Trust

  • Answer with numbers, a chart, the SQL used and the assumptions in plain Thai
  • Tell users when a question is outside the data's scope instead of guessing
  • Collect feedback on right and wrong answers to grow the query library
  • Lab: build a simple data chat page on Colab for classmates to try

Section 12: Workshop: Capstone Data Agent for a Business Team

  • Pick a scenario, such as sales, finance or customer service, and set the scope of questions
  • Design the full set of context, semantic layer, permissions and guardrails
  • Measure with the test question set and fix what is still wrong
  • Present the work, review it together and wrap up with a checklist before opening it to real users

Schedule & training options

For individuals — public rounds

No public rounds are open right now. Join the waiting list and we will contact you first when the next round opens, or ask us on LINE. Or call 02-570-8449 or 088-807-9770

For organisations — in-house / private

  • Tailor the content to your team’s tools and projects
  • Your dates, at your office or live online
  • Quotation with tax ID for procurement
Corporate training quote

Instructors

Frequently asked questions

What is text-to-SQL, and why does AI often write the wrong SQL?

Text-to-SQL means having an LLM turn a plain-language question into SQL and run it against a database to get the answer. It often goes wrong because the model does not know what tables and columns mean, does not know business metric definitions such as how net sales are calculated, and guesses joins incorrectly. Schema context, column descriptions, example queries and a semantic layer make answers far more accurate.

What is a semantic layer, and how does it help AI answer data questions accurately?

A semantic layer defines metrics, dimensions and table relationships in one place, for example how net revenue is calculated from which columns and which key joins orders to customers. When AI relies on these definitions instead of guessing from column names, its answers match the figures the business already uses and can be traced back.

How do you stop AI from running harmful SQL against a database?

First, connect the AI with a read-only role that can see only the tables or views it needs. Next, check the SQL before running it, for example allowing only SELECT statements, enforcing a LIMIT and setting a query timeout. Finally, show the SQL alongside the answer so users can verify it. These layers apply both to data agents you build yourself and to database access through MCP.

Who is the Data Agents and Text-to-SQL course for, and what should I know beforehand?

It suits data analysts and BI developers who field many data questions from the business, data engineers and backend developers asked to build AI data question tools, and DBAs or data governance teams who set permissions when AI accesses databases. You should write SQL including SELECT, JOIN, GROUP BY and subqueries, and write basic Python.

What will I be able to do after the Data Agents and Text-to-SQL course?

You will be able to build text-to-SQL with an LLM API and Python against PostgreSQL, prepare schema context and a semantic layer for more accurate answers, block unsafe queries with read-only roles and guardrails, measure accuracy with a test question set, and connect databases to AI assistants through MCP. Participants who complete the training receive a Certificate of Completion with its own number, which can be verified online.

Can the Data Agents and Text-to-SQL course be run in-house for our team?

Yes. It can be run in-house for your team, with examples adapted to your work, such as writing a data dictionary and test question set based on the data questions your business teams ask most. You can request a quotation from this page, or contact us on LINE @itgenius or by phone on 02-570-8449.

Related articles

Articles are published in Thai.