26/30
Part 5 of 5 · The worked exampleLearn

What interviewers ask next

Test generated SQL with six checks that need no answer key

How would you test generated SQL before trusting it? The fixture is the first answer. The second is a set of checks that need no answer key, so they work on production data the first time you run the query:

  • Rows across joins. Count before and after each join. A join to a lookup on its key keeps the count. A rise is a fan-out, a drop is an inner join or a filter.
  • The grain holds. In the output, COUNT(*) equals COUNT(DISTINCT key) for the key the question names.
  • Nothing is missing. The output has a row for every group in the base table, every city in customers.
  • Parts add up. Per-group totals sum to a total computed straight from the source table, with no joins.
  • Values are plausible. Rates sit between 0 and 1, and not all at one end. Counts don't exceed the population.
  • NULLs are counted. For every column you filter, join or compare on, you know how many NULLs it holds.

The parts-add-up check catches the worked example with no answer key:

SELECT SUM(total_cents) / 100.0 AS march_revenue
FROM orders
WHERE placed_at >= '2026-03-01' AND placed_at < '2026-04-01';
march_revenue
76.0

The generated query's cities added up to 86.0. That 86.0 holds two bugs pulling in opposite directions:

DollarsWhere it comes from
76.0March revenue, straight from orders
+25.0order 10's total, counted a second time by the fan-out
-15.0order 13, cut off by BETWEEN
86.0what the generated query's cities add up to

Here they don't cancel, so the total catches them. Two bugs can cancel exactly in a total, so a total is one check among several, never proof.

Comparing results is how OpenAI says it evaluates its own in-house data agent: "we compare both the SQL and the resulting data" (1 source). Reading the SQL tells you what to suspect. Only the data tells you what it returned.