Skill: SQL review for correctness

Review SQL for correctness risks—filters, indexes, and dangerous updates—before queries hit production.

Published 2026-11-09 · Translation: VI

What to do first

Review Map

Review Map

  • Restate the business question the SQL is supposed to answer before judging the query.
  • Flag UPDATE/DELETE without WHERE (or with overly broad predicates) as blockers: never “cleanup.”
  • Treat the run as unfinished until there is a correctness verdict, a grain sentence, and dangerous writes cleared or blocked.

Agents generate plausible SQL that is subtly wrong: joins that fan out revenue, filters that drop the wrong rows, deletes that touch the whole table. This skill forces a correctness and safety pass before production. Performance notes are allowed, but they stay separate from “does this answer the question safely?”

Part 1: Restate the question and the grain

Review this SQL. Do not rewrite it until findings are listed.

BUSINESS QUESTION: [one sentence]
SQL:
[paste]
SCHEMA NOTES (optional): [keys, known indexes, or "unknown"]

First return:
1. Restated question in your words
2. Row grain: what one output row represents
3. Join keys used and whether fanout is possible

Part 2: Severity-tagged findings

Emit findings with severity:
- BLOCKER: unbounded UPDATE/DELETE; wrong join that changes meaning; drops required filter
- MAJOR: fanout inflating metrics; timezone/collation footgun; missing predicate on tenant_id
- MINOR: readability; optional index suggestion

Rules:
1. Do not invent indexes: verify or mark UNKNOWN.
2. Prefer readable CTEs when they reduce bug risk; do not “clever” the meaning away.
3. Separate correctness notes from performance notes.
4. For writes: state blast radius (approx rows) or say UNKNOWN and block apply advice.

Part 3: Traps and skill file

Failure modes to catch early

Failure modes to catch early

# Skill: sql-review
## When
- Reviewing SQL/DML before production or agent-authored queries
## Hard rules
1. Dangerous writes flagged as blockers.
2. Grain/join checked against the stated question.
3. Don’t invent indexes.
## Steps

![Verdict Gate](/images/articles/skill-for-sql-review/en/02-verdict-gate.jpg)

*Verdict Gate*

1. Restate question + grain.
2. Filters/joins/fanout.
3. Write safety + severity-tagged findings.
## Done when
- Verdict + findings; performance notes separated.

Test cases

  1. UPDATE without WHERE. Pass: blocker.
  2. Join that duplicates revenue rows. Pass: major.
  3. Simple selective SELECT. Pass: can clear quickly with grain stated.

Related: write Cursor skills. For a NOT IN filter, run it on rows you inserted that already hold one blank: NOT IN and one NULL.

Common mistakes

  • Rewriting to window functions that change meaning.
  • Assuming indexes exist because column names “look indexed.”
  • Mixing “slow” with “wrong” in one paragraph.

You are done when the verdict answers the business question, grain is explicit, and no unbounded write slipped through.

Share

X Facebook LinkedIn