SQL Pro
Write, optimize, and troubleshoot SQL; design and migrate schemas across dialects.
When to use
Slow queries, complex joins/aggregations, window functions, indexing strategy, query-plan analysis, schema design/migration.
Core workflow
- Schema analysis — structure, indexes, query patterns, bottlenecks.
- Design — set-based operations with CTEs, window functions, appropriate joins.
- Optimize — analyze plans, add covering indexes, eliminate table scans.
- Verify — run
EXPLAIN ANALYZE; confirm no seq scans on large tables; iterate until target met.
- Document — explanations, index rationale, performance metrics.
Quick-reference patterns
-- CTE: latest completed order per customer
WITH ranked AS (
SELECT customer_id, order_id, total_amount,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
FROM orders WHERE status='completed') -- filter early
SELECT customer_id, order_id, total_amount FROM ranked WHERE rn = 1;
-- Window: running total + rank, no self-join
SELECT department_id, employee_id, salary,
SUM(salary) OVER (PARTITION BY department_id ORDER BY hire_date) AS running_payroll,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank
FROM employees;
-- Correlated subquery (slow) -> aggregation join (fast)
-- BEFORE: one subquery per row
-- AFTER:
SELECT o.order_id, COALESCE(agg.item_count,0) AS item_count
FROM orders o
LEFT JOIN (SELECT order_id, SUM(quantity) AS item_count FROM order_items GROUP BY order_id) agg
ON agg.order_id = o.id;
EXPLAIN ANALYZE — check: Seq Scan on large table → add/fix index; actual rows ≫ estimated → ANALYZE; high Buffers read vs hit → missing cache/index.
Constraints
MUST: analyze plans before optimizing; use set-based over row-by-row; filter early (before joins); use EXISTS over COUNT for existence; handle NULLs explicitly; create covering indexes for frequent queries; test at production-scale volumes.
MUST NOT: SELECT * in production; cursors where set-based works; ignore dialect-specific optimizations; ignore data volume/cardinality.
Output
- Optimized query with inline comments
- Required indexes + rationale
- Execution-plan analysis
- Before/after metrics
- Platform-specific notes if relevant
1---2name: sql-pro3description: "Use when a query is slow, you need complex joins/aggregations/window functions, or you're designing or migrating a schema. Covers CTEs, window functions, indexing, EXPLAIN/ANALYZE interpretation, before/after benchmarking, and dialect migration (PostgreSQL, MySQL, SQL Server, Oracle). Triggers: SQL optimization, query performance, database design, window functions, CTEs, query tuning, EXPLAIN plan, indexing."4version: 1.0.05license: MIT6tags: [sql, query-optimization, ctes, window-functions, indexing, schema-design, explain, dialects]7source: https://github.com/jeffallan/claude-skills/tree/main/skills/sql-pro8derived_from: awesomeclaude9metadata:10 source: https://skillmd.com/skills/aashutosh396/sql-pro11---1213# SQL Pro1415Write, optimize, and troubleshoot SQL; design and migrate schemas across dialects.1617## When to use1819Slow queries, complex joins/aggregations, window functions, indexing strategy, query-plan analysis, schema design/migration.2021## Core workflow22231. **Schema analysis** — structure, indexes, query patterns, bottlenecks.242. **Design** — set-based operations with CTEs, window functions, appropriate joins.253. **Optimize** — analyze plans, add covering indexes, eliminate table scans.264. **Verify** — run `EXPLAIN ANALYZE`; confirm no seq scans on large tables; iterate until target met.275. **Document** — explanations, index rationale, performance metrics.2829## Quick-reference patterns3031```sql32-- CTE: latest completed order per customer33WITH ranked AS (34 SELECT customer_id, order_id, total_amount,35 ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn36 FROM orders WHERE status='completed') -- filter early37SELECT customer_id, order_id, total_amount FROM ranked WHERE rn = 1;3839-- Window: running total + rank, no self-join40SELECT department_id, employee_id, salary,41 SUM(salary) OVER (PARTITION BY department_id ORDER BY hire_date) AS running_payroll,42 RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank43FROM employees;4445-- Correlated subquery (slow) -> aggregation join (fast)46-- BEFORE: one subquery per row47-- AFTER:48SELECT o.order_id, COALESCE(agg.item_count,0) AS item_count49FROM orders o50LEFT JOIN (SELECT order_id, SUM(quantity) AS item_count FROM order_items GROUP BY order_id) agg51 ON agg.order_id = o.id;52```5354EXPLAIN ANALYZE — check: Seq Scan on large table → add/fix index; actual rows ≫ estimated → `ANALYZE`; high Buffers `read` vs `hit` → missing cache/index.5556## Constraints5758MUST: analyze plans before optimizing; use set-based over row-by-row; filter early (before joins); use EXISTS over COUNT for existence; handle NULLs explicitly; create covering indexes for frequent queries; test at production-scale volumes.59MUST NOT: `SELECT *` in production; cursors where set-based works; ignore dialect-specific optimizations; ignore data volume/cardinality.6061## Output62631. Optimized query with inline comments642. Required indexes + rationale653. Execution-plan analysis664. Before/after metrics675. Platform-specific notes if relevant