# How to test AI-generated SQL

> Check AI-generated SQL against a small dataset with independently known totals. Exercise joins, missing values and duplicates before trusting the result.

Source: https://cueapril.com/guides/testing-generated-sql
Updated: 2026-12-01
Index: https://cueapril.com/llms.txt

## What catches a plausible query that joins or aggregates the wrong rows?

Check the result against independently calculated rows and totals. A syntactically valid query can silently multiply or discard business events.

A report joins orders to shipments and sums revenue.

## Work through the decision

One order split into two shipments is counted twice. A second order with no shipment disappears with an inner join.

Steps and evidence for the reviewer

| Step | Evidence to keep |
| --- | --- |
| Construct counterexamples | Use one split order and one unshipped order. |
| Compute the oracle | Write down revenue per order before running the query. |
| Inspect row multiplicity | Compare per-order results before comparing the total. |

## What changes the decision

Keep the seed, expected rows and query together. Aggregate equality on a larger sample can conceal overcounts and undercounts that cancel.

## Primary documentation

- [PostgreSQL documentation on modifying tables](https://www.postgresql.org/docs/current/ddl-alter.html)

By Cue April. Updated 2026-12-01.

## Related

- [Testing guides](https://cueapril.com/guides): Learn how to select regression checks, verify generated tests and read failure evidence. Use examples that connect a code change to user behaviour.
- [Developer quickstart](https://cueapril.com/guides/agentic-testing-quickstart): Connect Claude Code or Codex to Cue April, check your project is ready and test an approved GitHub pull request. Match the findings to the exact commit.
- [Risk-based testing](https://cueapril.com/guides/risk-based-testing): Build a risk-based test strategy with an impact and likelihood matrix. Use three worked examples to choose checks and document a release exception.
