SQL Dojo
Pattern

Evaluating a text-to-SQL agent with a golden set

Score a data agent on questions with checked answers: compare results in any order, within a tolerance, on several periods, then triage the failures.

A golden set measures a data agent: a list of questions, each with the answer a person checked. Store the expected results and the agent's, one row per result row (question_id, row_key, value, and copy: ROW_NUMBER() per question and key), then grade them in SQL:

SELECT COALESCE(e.question_id, a.question_id) AS question_id,
       MIN(CASE WHEN ABS(a.value - e.value) <= 0.005 * ABS(e.value) THEN 1 ELSE 0 END) = 1 AS passed
FROM expected e
FULL JOIN answered a ON a.question_id = e.question_id
  AND a.row_key IS NOT DISTINCT FROM e.row_key AND a.copy = e.copy
GROUP BY COALESCE(e.question_id, a.question_id);

A missing, extra or repeated row finds no partner and fails its question, as does a declined one; written as a multiplication, the tolerance accepts only 0 for an expected 0.

The trap. Grading the number instead of the query. A grader that compares rows in order and to the cent fails right answers; one number on one period passes wrong queries whose two errors cancel out. Ask each question on several periods, pin the expected answers to a snapshot of the data, and label every failure (fan-out, wrong date column, dropped filter, average of ratios, invented value, unfinished period, stale table): each label points to a fix in the model or its descriptions, not in the prompt.

On your warehouse. IS NOT DISTINCT FROM runs on PostgreSQL, Snowflake, BigQuery, Databricks and DuckDB. Store the SQL of frequent questions as verified queries: Snowflake's Cortex Analyst keeps them in its semantic model.

WITH golden(question_id, row_key, value) AS (VALUES
  ('q1', 'DE', 80.0), ('q1', 'FR', 120.0),
  ('q2', 'card', 50.0),
  ('q3', 'Jan', 10.0),
  ('q4', 'Jan', 7.0)),
agent(question_id, row_key, value) AS (VALUES
  ('q1', 'FR', 120.0), ('q1', 'DE', 80.2),     -- another order, 0.25 % off
  ('q2', 'card', 50.0), ('q2', 'card', 50.0),  -- a row repeated
  ('q3', 'Jan', 10.5)),                        -- 5 % off, and q4 declined
e AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY question_id, row_key ORDER BY value) AS copy FROM golden),
a AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY question_id, row_key ORDER BY value) AS copy FROM agent)
SELECT COALESCE(e.question_id, a.question_id) AS question_id,
       MIN(CASE WHEN ABS(a.value - e.value) <= 0.005 * ABS(e.value) THEN 1 ELSE 0 END) = 1 AS passed
FROM e
FULL JOIN a
  ON a.question_id = e.question_id
 AND a.row_key IS NOT DISTINCT FROM e.row_key
 AND a.copy = e.copy
GROUP BY COALESCE(e.question_id, a.question_id)
ORDER BY question_id;
question_idpassed
q1true
q2false
q3false
q4false
Try it in Randori