SQL Query Fixes and Safe Migrations
Get schema-grounded SQL, evidence-led query tuning, and staged ORM migrations with concrete validation checks.
Before & after
Same prompt, same agent — the only difference is the skill.
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?
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
---
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)More development skills
View all Development skills →Performance Bottleneck Profiling: Node, Python, Go
Classifies a slowdown, gives exact profiler commands, reads the output, and proves the fix with same-conditions p95/p99 numbers.
Feature Scaffolding with Working Tests
Turn a short feature spec into code, runnable tests, and integration wiring that match your existing project.
Feature Flag Rollouts and Kill Switches
Design staged flag rollouts, verify rollback paths, and audit flag debt with explicit defaults, gates, and retirement plans.
Microcontroller Firmware and RTOS Engineering
Build and debug MCU firmware with bounded ISR/RTOS timing, safe DMA ownership, and measured power budgets.