THE GITHTML FIELD GUIDE

Check an AI-written SQL explanation against query semantics

Verify an AI-written SQL explanation against the exact query and database dialect, then use a small safe fixture to test the claimed result. Pay particular attention to joins, nulls, grouping, ordering, and limits. A query returning plausible rows does not prove the explanation is correct, and a read-looking statement can still invoke functions or behavior requiring review.

Translate the prose into claims about rows

Extract statements such as returns every customer, keeps unmatched rows, removes duplicates, or selects the newest record. Identify the precise SQL fragment each claim depends on. Record the database engine and version, because similar syntax can differ across systems. Inspect the entire statement, including common table expressions and called functions, before deciding it is safe to execute. Do not run generated SQL against production merely to validate a paragraph. The review should first establish what the statement is intended to read or change and which data access is authorized.

Construct a fixture that can disprove the explanation

Use a disposable dataset with carefully chosen edge cases: unmatched rows, duplicate keys, null values, ties in ordering, and an empty input. Write down the expected result manually before executing the query in an approved test environment. The checklist below separates the AI claim from the fixture and observed rows. Check filters applied before and after joins, aggregate behavior, and whether ordering is explicit when the prose promises a sequence. A tiny counterexample is often more informative than a large realistic dataset whose result looks reasonable. Consult the database's primary SQL reference for disputed semantics instead of asking the same model to reassure you.

text
Claim about returned rows:
Dialect and version:
Relevant SQL fragment:
Fixture with nulls / duplicates / unmatched rows:
Expected result:
Observed result:
Ordering assumptions:
Corrected explanation:

Correct the explanation and preserve the counterexample

Rewrite the prose to state exactly what the query does, including conditions under which rows are excluded or results are not ordered. If the query itself is wrong, report that separately; a documentation review does not automatically authorize changing application behavior. Keep the safe fixture or test with the review evidence so future edits can repeat the check. Avoid performance promises unless a separate measured investigation supports them. When the query changes, revisit the explanation even if the output columns remain identical. The final document should teach the reader to predict the result, not merely narrate the names of SQL keywords in fluent language.

  1. Map each prose claim to a specific SQL operation and record the dialect.
  2. Review side effects and use only an approved disposable test context.
  3. Create edge-case rows that could contradict the claim before running the query.
  4. Preserve counterexamples and distinguish documentation corrections from proposed query changes.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All working with ai-written docs guides →

Read as MarkdownAll guides

Check an AI-written SQL explanation against query semantics · gitHtml