Mirage Academy · Course · Advanced · SQL

Text to SQL course: build an analyst that writes safe SQL

Natural Language to SQL & Conversational Analytics is an advanced course for software engineers and technical product people who build AI features. Your colleagues ask data questions in plain language. You build the analyst that answers them: it understands the schema and your business definitions, validates its SQL before anything runs, executes under permissions and row-level security, and explains the result with evidence.

Open the courseBeginner sibling: AI data analytics course

The loop

Four parts of one loop each replace a guess with a check

Watch the intro video, then read the loop as four pairs: what a model would guess, and the check that stands in its place.

Intro video
Still from the intro video for the course Natural Language to SQL & Conversational Analytics
Intro video, 1 min 12 s. Transcript below.
Read the transcript

Your colleagues ask data questions in plain language. A model can write the SQL, but it may guess a definition, read rows it should not, or give an answer nobody can check. This course is for whoever must make that safe. The course builds one loop. First, the analyst understands the schema and your business definitions. Then it generates SQL and validates it before anything runs. Next, it executes under permissions and row-level security. Finally, it explains the result with evidence. Each part replaces a guess with a check. Terms come from a semantic layer. SQL is validated before it runs. Queries respect each user's permissions. Answers carry evidence, and you evaluate the whole system. If you build or run analytics and want a model to answer data questions safely, start with chapter one: how text-to-SQL works.

  1. The model guesses what a business term means.Terms come from a semantic layer.
  2. The model writes SQL that may be wrong.SQL is validated before it runs.
  3. The query reads rows a person should not see.Queries respect each user's permissions.
  4. The answer is a number nobody can check.Answers carry evidence, and you evaluate the whole system.
  1. 01

    Schema and business definitions

    The analyst understands the schema and your business definitions.

  2. 02

    Generate and validate

    It generates SQL and validates it before anything runs.

  3. 03

    Permissions and row-level security

    It executes under permissions and row-level security.

  4. 04

    Evidence

    It explains the result with evidence.

Four rows from guess to check, one for each part of the loop: definitions, validation, permissions and evidence.
  • In the intro video, the course points out that a model can write the SQL but may guess a definition, read rows it should not, or give an answer nobody can check. It is for whoever must make that safe.

Chapter 1

A query is judged by its result, so two different queries can both be right

Two example queries are written differently and return the same result. The table and numbers are made up.

Example

Query ASELECT COUNT(*) FROM orders WHERE status = 'paid'Query BSELECT SUM(1) FROM orders o WHERE o.status IN ('paid')Result of both
orders
3

The text differs and the result matches, so the answer counts as right.

  1. 01

    The Six-Stage Pipeline and Its Latency Budget

    Lesson 1 of 5 in this chapter.

  2. 02

    What Goes Into the Schema Context

    Lesson 2 of 5 in this chapter.

  3. 03

    Four Ways a Good-Looking Query Is Wrong

    Lesson 3 of 5 in this chapter.

  4. 04

    Judging an Answer by Result Match

    Lesson 4 of 5 in this chapter.

  5. 05

    Scope Decisions and a First Baseline

    Lesson 5 of 5 in this chapter.

Two differently written example queries pointing at one result table, with a verdict line.

Chapter 2

The model sees keys, join paths and descriptions, and a pruned schema

A small example schema. One cryptic column carries a description, and a table the question does not need is dimmed.

Example schema

  • orders

    • PKid
    • FKcustomer_id
    • amt_cdescription added: order amount in cents
  • customers

    • PKid
    • region
  • audit_log

    • PKid
    • event
    left out for this question

orders.customer_id joins customers.id(foreign key to primary key)

  1. 01

    Asking the database about itself

    Lesson 1 of 5 in this chapter.

  2. 02

    Keys and join paths

    Lesson 2 of 5 in this chapter.

  3. 03

    Descriptions for cryptic columns

    Lesson 3 of 5 in this chapter.

  4. 04

    Sample values and cardinality

    Lesson 4 of 5 in this chapter.

  5. 05

    Pruning the schema

    Lesson 5 of 5 in this chapter.

An example schema of three tables: two used and joined by a key, one pruned, and one cryptic column with a description.

Chapter 3

Same question, two numbers: a metric definition picks one

An example question with two honest answers, and the definition that settles which one the analyst gives.

What was revenue last month?Example

  • GrossSUM(amount)
  • Net of refundsSUM(net_amount)
Metric in the semantic layer
  • name: revenue
  • means: net of refunds
  • dimension: month
One definition, one number
  1. 01

    Same question, two numbers

    Lesson 1 of 5 in this chapter.

  2. 02

    Writing a metric

    Lesson 2 of 5 in this chapter.

  3. 03

    Dimensions and synonyms

    Lesson 3 of 5 in this chapter.

  4. 04

    Governed views

    Lesson 4 of 5 in this chapter.

  5. 05

    Testing the layer

    Lesson 5 of 5 in this chapter.

An example question that splits into two possible answers and a metric definition that picks one.

Chapter 4

Generate the SQL, then check its text with static validation before it runs

Pick an example query and a role. The text check lists the rules the query fails, and a passing query runs on an example table.

Check a query, then run it as a role

Made-up queries, orders and roles, shown only to explain the method. This is a small example, not a production validator.

Query
Role
SELECT region, SUM(amount) AS total FROM orders GROUP BY region LIMIT 100
  • No comment markers: passed
  • One statement only: passed
  • Starts with SELECT or WITH: passed
  • No write or schema keyword: passed
  • Only allowed tables: passed
  • Row cap of at most 100: passed

Orders table · Rows this role may see: 4 / 8

  • #1East120
  • #2East640
  • #3West310
  • #4West780
  • #5East90
  • #6West450
  • #7East530
  • #8West60

Result

The text check passed, so the query runs.

regiontotal
East1380

The rule: the text check passes a query only when it holds no comment marker, is a single statement, starts with SELECT or WITH, contains no write or schema keyword, reads only allowed tables and carries a row cap of at most 100. It reads text only. In this example a role with a region sees only that region's rows, whatever the query says. Row-level security itself is the topic of chapter 5.

  1. 01

    The generation prompt

    Lesson 1 of 5 in this chapter.

  2. 02

    Static validation

    Lesson 2 of 5 in this chapter.

  3. 03

    EXPLAIN as a guard

    Lesson 3 of 5 in this chapter.

  4. 04

    Retry with feedback

    Lesson 4 of 5 in this chapter.

  5. 05

    Testing the validator

    Lesson 5 of 5 in this chapter.

An example with six queries and three roles: a text check lists the failed rules, and a passing query runs on the example rows that role may see.

Chapter 5

Safety sits in layers around the data: permissions, limits and row-level security

Three lessons name the layers around the data. Two more cover prompt injection through data and personal data.

Example drawing

Least privilegeTime and size limitsRow-level securityThe data
  1. 01

    Least privilege

    Lesson 1 of 5 in this chapter.

  2. 02

    Time and size limits

    Lesson 2 of 5 in this chapter.

  3. 03

    Row-level security

    Lesson 3 of 5 in this chapter.

  4. 04

    Prompt injection through data

    Lesson 4 of 5 in this chapter.

  5. 05

    Personal data

    Lesson 5 of 5 in this chapter.

Three nested frames named least privilege, time and size limits, and row-level security, around the data at the center.

Chapter 6

The numbers in an answer come from the result, and the answer points at them

An example answer in which each number points to a cell of the result, followed by a caveat.

The answerExample

East revenue in March was 1,380. West revenue was 1,600.

The result it came from
regionrevenue
East1,380
West1,600

CaveatRevenue here means net of refunds, as the metric defines it.

  1. 01

    The evidence package

    Lesson 1 of 5 in this chapter.

  2. 02

    Numbers come from the result

    Lesson 2 of 5 in this chapter.

  3. 03

    Caveats

    Lesson 3 of 5 in this chapter.

  4. 04

    Rounding and percentages

    Lesson 4 of 5 in this chapter.

  5. 05

    Definitions and ambiguity

    Lesson 5 of 5 in this chapter.

An example answer whose two numbers carry markers that point to two cells of the result table, with a caveat under it.

Chapter 7

The same score means less on a small evaluation set

Two example sets with the same share correct. The range shows how far each score can be trusted.

Range for the true share (about 95 percent)Example sets

  • Small set18 / 20 correct · 70 to 97 %
  • Larger set180 / 200 correct · 85 to 93 %

Both sets score 90 percent. The small set cannot rule out a much lower true score.

  1. 01

    Building the set

    Lesson 1 of 5 in this chapter.

  2. 02

    Metrics

    Lesson 2 of 5 in this chapter.

  3. 03

    How much to trust a small set

    Lesson 3 of 5 in this chapter.

  4. 04

    Comparing runs

    Lesson 4 of 5 in this chapter.

  5. 05

    Monitoring

    Lesson 5 of 5 in this chapter.

Two example evaluation sets with the same share correct, drawn as a wide range and a narrow range on one scale.

Chapter 8

The capstone is a safe conversational analyst, ending in a release decision

You assemble, harden and evaluate the analyst, write its user guide and decide on its release.

Release checklist
  1. Assemble

    Lesson 1 of 5 in this chapter.

  2. Harden

    Lesson 2 of 5 in this chapter.

  3. Evaluate

    Lesson 3 of 5 in this chapter.

  4. Write the user guide

    Lesson 4 of 5 in this chapter.

  5. DecisionRelease decision

    Lesson 5 of 5 in this chapter.

Capstone: a safe conversational analystOpen the course: Natural Language to SQL & Conversational Analytics

The capstone as a release checklist of five rows, the last one a decision.

How you learn here

You explain your design to an AI coach, and the course comes with a certificate

AI coach
You explain your design decisions to the coach out loud. The coach is an AI, and it comes back with a question wherever your reasoning is vague.
Certificate
Meet the rules and the course comes with a certificate. Its check code can be entered on a public page, which says whether the certificate is genuine.
Limits
According to the Get certified page, the certificate is not a state-recognized qualification. The AI coach does not decide your levels and cannot tell you whether you will be certified.
Three facts about learning here: the AI coach, the certificate and what the certificate is not.

What the certificate rules say →

Where to go next

Read next: business analysis, data pipelines or prompts

Three pages are close to this course.

  • AI-Powered Data Analytics & Business Intelligence

    • Course

    The business side: a beginner course on answering business questions with data and AI, and telling evidence from speculation. The catalog says no programming is needed.

  • Data Engineering for AI Applications

    • Course

    The data side: an advanced course on building an ingestion, transformation and retrieval pipeline that keeps AI applications fed with good data.

  • Prompt engineering course guide

    • Topic page

    The prompt side: how to write instructions for a model, which matters for the generation prompt in chapter four.

The beginner sibling course on analytics, a course on data pipelines and the topic page on prompts.

AI data analytics course →Data engineering for AI course →Prompt engineering course guide →

Questions about the Natural Language to SQL & Conversational Analytics course

  • What is the Natural Language to SQL & Conversational Analytics course?

    An advanced course in which you build a conversational data analyst that writes safe SQL and explains its evidence. It runs for eight chapters and finishes with a capstone.

  • Who is the course for?

    The course is for engineers and technical product people who build AI features. The catalog marks it as advanced.

  • What is the conversational analyst capstone?

    A safe conversational analyst. You assemble it, harden it, evaluate it, write the user guide and make the release decision.

  • How is the generated SQL kept safe?

    Chapter four covers static validation, EXPLAIN as a guard, retry with feedback and testing the validator. Chapter five covers least privilege, time and size limits, row-level security, prompt injection through data and personal data.

  • How does the analyst explain its answers?

    Chapter six covers the evidence package, why numbers come from the result, caveats, rounding and percentages, and definitions and ambiguity.

  • How is this different from the AI data analytics course?

    This course is advanced and is for people who build the analyst. The AI data analytics course is a beginner course for professionals who answer business questions with data and AI, and no programming is needed there.

  • Does the Text to SQL course give a certificate?

    Yes. A check code on it can be looked up on the public check page. The Get certified page says it carries no state recognition.

  • How does the coach handle my SQL reasoning?

    You explain your design decisions by voice, and the coach, which is an AI, asks again where an explanation is unclear. It cannot tell you whether you will be certified.