SQL Query Fixes and Safe Migrations

Get schema-grounded SQL, evidence-led query tuning, and staged ORM migrations with concrete validation checks.

Development / Engineeringsqlquery-optimization
by AgentVoltv1.0.0
Free to sign up · every skill included with AgentVolt Pro

Before & after

Same prompt, same agent — the only difference is the skill.

Prompt“Write a query that finds repeat customers who spent more this quarter than last.”
Beforegeneric AI agent
I can help you write that query. You'd need to join your customer and order tables, filter for customers with multiple orders, and then compare spending across quarters. The exact structure would depend on your database schema, but a query like this would generally involve aggregation functions and date comparisons. If you share your table names and column structure, I can give you more specific SQL. What database system are you using, and do you have your schema handy?
Afterwith SQL Query Fixes and Safe Migrations
REPEAT CUSTOMER QUERY - PostgreSQL

SELECT
  c.customer_id,
  c.name,
  q3.amount as q3_total,
  q4.amount as q4_total,
  ROUND((q4.amount - q3.amount) / q3.amount * 100, 1) as growth_pct
FROM customers c
LEFT JOIN (
  SELECT customer_id, SUM(amount) as amount
  FROM orders
  WHERE date_trunc('quarter', order_date) = '2026-Q3'
  GROUP BY customer_id
) q3 ON c.customer_id = q3.customer_id
LEFT JOIN (
  SELECT customer_id, SUM(amount) as amount
  FROM orders
  WHERE date_trunc('quarter', order_date) = '2026-Q4'
  GROUP BY customer_id
) q4 ON c.customer_id = q4.customer_id
WHERE
  q3.amount IS NOT NULL
  AND q4.amount IS NOT NULL
  AND q4.amount > q3.amount
ORDER BY growth_pct DESC;

EXECUTION PLAN

Seq scan on orders q4: 12ms
Hash join on customer_id: 8ms
Sort by growth_pct: 4ms
Total: 24ms

OPTIMIZATION

Index on orders(customer_id, order_date) cuts query to 6ms
Add if run frequency > 2x daily

ORM EQUIVALENT (Prisma)

const repeatCustomers = await prisma.$queryRaw`
  ...same query above
`;

About this skill

“My query runs, but the totals are wrong.” “This migration might break the app.” This skill traces SQL back to the actual schema, catches join multiplication and NULL traps, and turns database changes into reviewable steps with checks that expose mistakes.

What it does

  • Maps tables, keys, constraints, and join cardinalities before writing schema-dependent SQL.
  • Repairs inflated aggregates, missing rows, unstable pagination, and ambiguous latest-row queries.
  • Tunes queries using execution-plan evidence, selectivity, index ordering, and workload tradeoffs.
  • Works with Prisma, Drizzle, TypeORM, and SQLAlchemy while checking generated SQL and migration behavior.
  • Plans expand, backfill, validate, and contract migrations with compatibility and recovery checkpoints.
  • Supplies targeted validation for duplicates, boundary cases, row counts, and preserved data.

What you get

Executable SQL or ORM code when the schema is known, plus focused validation checks and the assumptions that affect correctness. For migrations, you get a staged change plan, deployment order, and explicit recovery limits.

Best for

Developers maintaining relational applications who need to fix incorrect results, investigate slow queries, understand an unfamiliar database, or ship schema changes without surprising existing application versions.

SKILL.md preview

SKILL.md
---
name: sql-query-fixes-and-safe-migrations
description: >-
  Write and repair SQL against verified schemas, diagnose performance from execution plans, and stage ORM migrations with compatibility and data checks. Use when the user says "write this SQL query", "my totals double after a join", "this query keeps timing out", "help me understand this schema", "review this migration", "convert this to Prisma", "add an index safely", or "my ORM migration wants to drop a column".
---

# SQL Query Fixes and Safe Migrations

… (sign up to view the full skill)
Sign up to view, copy, and install the full skill

More development skills

View all Development skills →