AI Engineering ยท

Validate AI-Generated SQL Before It Hits Production

AI can draft SQL fast. The hard part is deciding what to trust. This workshop gives your team a repeatable path from AI suggestion to production-safe query.

Teams have debated ORM vs raw SQL for years. In production, that debate usually ends the same way: when systems grow, teams care about query plans, latency, and incident risk, not ideology.

AI is now good enough to draft SQL quickly. The hard part is deciding what to trust. So instead of arguing abstractions, this workshop focuses on what actually matters: a repeatable path from AI suggestion to production-safe query.

What You Will Learn

You will learn how to operationalize AI-assisted SQL with measurable standards: correctness, compute effort, and performance. The walkthrough includes a starter repository and a concrete workflow your team can adopt for PR reviews and release gates.

Workshop: Validate AI-Generated SQL in 10 Minutes

If you want this to be practical and not theoretical, run the workshop starter repo.

Repo:github.com/Eberewill/sql-ai-workshop

Step 1: Clone and Configure

git clone https://github.com/Eberewill/sql-ai-workshop.git cd sql-ai-workshop cp .env.example .env

Set your database connection in .env :

DATABASE_URL=postgres://user:password@localhost:5432/your_db WORKSHOP_QUERY_NAME=active-users-report BENCHMARK_RUNS=15

Step 2: Install and Run

npm install npm run workshop

Step 3: What Happens Under the Hood

The workshop pipeline runs automatically through six stages:

  1. 1. Generate SQL candidates (src/sql/candidates/generateCandidates.ts)

  2. 2. Validate correctness against baseline (src/sql/validation/correctness.ts)

  3. 3. Run safety checks (src/sql/validation/safety.ts)

  4. 4. Profile compute cost via EXPLAIN ANALYZE (src/sql/validation/explain.ts)

  5. 5. Benchmark latency at p50 and p95 (src/sql/validation/benchmark.ts)

  6. 6. Pick winner and write decision record (src/sql/docs/writeDecisionDoc.ts)

Step 4: Review the Output

You will get two outputs after each run:

  • A console summary showing the winning query and its performance numbers

  • A markdown decision file written to docs/sql-decisions/

That decision doc includes:

  • Intent of the query

  • All candidate SQLs that were evaluated

  • Pass or fail result for correctness and safety

  • Compute and performance metrics

  • The selected query and the reason it was chosen

Why This Matters

AI can generate SQL fast.

But fast is only useful if the output is also safe, correct, and efficient.

That is what this workshop operationalizes:

  • Correct: the query returns the right data against known baselines

  • Safe: static analysis flags destructive or unbounded operations

  • Efficient: EXPLAIN ANALYZE surfaces sequential scans, missing indexes, and high-cost plans

  • Documented: every decision is written down, not just remembered

That is the real production standard. Fast output you cannot verify is a liability, not an asset.

Making This Part of Your Team Workflow

Once you have run the workshop locally, the workflow translates directly into your engineering process.

Use it as a PR gate:

  • Any AI-suggested query must pass correctness and safety checks before review

  • The decision doc becomes part of the PR, not a separate Slack thread

  • Latency regressions are visible before merge, not after deploy

The goal is not to slow things down. It is to make the fast thing also the safe thing. When your validation pipeline takes under 10 minutes and produces a written record, there is no reason to skip it.

The Bigger Picture

AI-assisted development is not going away. The teams that build trust in AI output will move faster and with more confidence than teams that either reject it entirely or accept it uncritically.

Validation infrastructure is the difference between those two outcomes.

This workshop gives you the starting point. The repo is open, the workflow is repeatable, and the standards are measurable.

Run it, adapt it, and make it yours.