New to Claude Skills? Learn how to install them →

jeffallan on GitHub

SQL Pro

Free

Optimize SQL queries and design efficient database schemas.

Get this skill

Free · Opens the source repo

What SQL Pro does

SQL Pro is a skill designed to assist developers and database administrators in optimizing SQL queries, designing database schemas, and troubleshooting performance issues. It provides a structured workflow that begins with schema analysis, helping users identify potential performance bottlenecks and inefficient query patterns. The skill emphasizes the importance of set-based operations and offers guidance on using Common Table Expressions (CTEs) and window functions effectively. This makes it particularly useful for those dealing with complex queries or large datasets.

The optimization process includes analyzing execution plans and implementing strategies such as covering indexes to enhance query performance. Users are guided through verifying improvements with tools like EXPLAIN ANALYZE, ensuring that queries meet performance targets. The skill also emphasizes documentation, providing users with explanations of their queries, index rationale, and performance metrics. This comprehensive approach not only aids in immediate query optimization but also fosters better database design practices.

SQL Pro is ideal for developers and database professionals who need to write complex SQL queries, troubleshoot performance issues, or design and migrate database schemas. Whether you're working with PostgreSQL, MySQL, SQL Server, or Oracle, this skill helps navigate dialect-specific differences and provides best practices for each environment. With detailed reference guides included, users can quickly access relevant information based on their current task, making SQL Pro a valuable resource for enhancing SQL proficiency and database performance.

When to use it

Use SQL Pro when you need to optimize queries, design schemas, or troubleshoot performance issues in your database.

When not to use it

This skill may not be suitable for simple queries or basic database tasks that do not require advanced optimization techniques.

What you can build with it

Optimizing a Slow Query

A developer notices that a report-generating query is taking too long to execute. They use SQL Pro to analyze the execution plan and identify missing indexes, resulting in a significant performance improvement.

Designing a New Database Schema

A team is tasked with creating a new application and needs to design its database. They leverage SQL Pro to ensure the schema is well-structured and optimized for performance from the start.

Migrating Between SQL Dialects

A database administrator needs to migrate queries from MySQL to PostgreSQL. SQL Pro assists by providing insights into dialect differences and optimizing the queries for the new environment.

How to install SQL Pro

View source

1. Install with the skills CLI

npx skills add jeffallan/claude-skills/sql-pro --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by jeffallan

SQL Pro

Core Workflow

  1. Schema Analysis - Review database structure, indexes, query patterns, performance bottlenecks
  2. Design - Create set-based operations using CTEs, window functions, appropriate joins
  3. Optimize - Analyze execution plans, implement covering indexes, eliminate table scans
  4. Verify - Run EXPLAIN ANALYZE and confirm no sequential scans on large tables; if query does not meet sub-100ms target, iterate on index selection or query rewrite before proceeding
  5. Document - Provide query explanations, index rationale, performance metrics

Reference Guide

Load detailed guidance based on context:

TopicReferenceLoad When
Query Patternsreferences/query-patterns.mdJOINs, CTEs, subqueries, recursive queries
Window Functionsreferences/window-functions.mdROW_NUMBER, RANK, LAG/LEAD, analytics
Optimizationreferences/optimization.mdEXPLAIN plans, indexes, statistics, tuning
Database Designreferences/database-design.mdNormalization, keys, constraints, schemas
Dialect Differencesreferences/dialect-differences.mdPostgreSQL vs MySQL vs SQL Server specifics

Quick-Reference Examples

CTE Pattern

-- Isolate expensive subquery logic for reuse and readability
WITH ranked_orders 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, before the join
)
SELECT customer_id, order_id, total_amount
FROM ranked_orders
WHERE rn = 1;                           -- latest completed order per customer

Window Function Pattern

-- Running total and rank within partition — no self-join required
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;

EXPLAIN ANALYZE Interpretation

-- PostgreSQL: always use ANALYZE to see actual row counts vs. estimates
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT *
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.created_at > NOW() - INTERVAL '30 days';

Key things to check in the output:

  • Seq Scan on large table → add or fix an index
  • actual rows ≫ estimated rows → run ANALYZE <table> to refresh statistics
  • Buffers: shared hit vs read → high read count signals missing cache / index

Before / After Optimization Example

-- BEFORE: correlated subquery, one execution per row (slow)
SELECT order_id,
       (SELECT SUM(quantity) FROM order_items oi WHERE oi.order_id = o.id) AS item_count
FROM orders o;

-- AFTER: single aggregation join (fast)
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;

-- Supporting covering index (includes all columns touched by the query)
CREATE INDEX idx_order_items_order_qty
    ON order_items (order_id)
    INCLUDE (quantity);

Constraints

MUST DO

  • Analyze execution plans before recommending optimizations
  • Use set-based operations over row-by-row processing
  • Apply filtering early in query execution (before joins where possible)
  • Use EXISTS over COUNT for existence checks
  • Handle NULLs explicitly in comparisons and aggregations
  • Create covering indexes for frequent queries
  • Test with production-scale data volumes

MUST NOT DO

  • Use SELECT * in production queries
  • Use cursors when set-based operations work
  • Ignore platform-specific optimizations when targeting a specific dialect
  • Implement solutions without considering data volume and cardinality

Output Templates

When implementing SQL solutions, provide:

  1. Optimized query with inline comments
  2. Required indexes with rationale
  3. Execution plan analysis
  4. Performance metrics (before/after)
  5. Platform-specific notes if applicable

Documentation

Frequently asked questions about SQL Pro

Similar skills