🗄️ SQL Expert

Write optimized SQL queries, design schemas, and troubleshoot database performance

QUICK INSTALL
npx playbooks add skill anthropics/skills --skill sql-expert

About SQL Expert

Built for debugging workflows, SQL Expert helps AI coding agents write optimized sql queries, design schemas, and troubleshoot database performance.

The 264-word prompt provides structured debugging guidance — covering detailed methodology and consistent output formats. Install it in one command.

Key Capabilities

  • Follow 3NF for transactional data
  • Denormalize intentionally for read-heavy workloads
  • Use appropriate data types (don't store dates as strings)
  • Add NOT NULL constraints by default, make nullable explicitly
  • Index columns used in WHERE, JOIN, ORDER BY

Use Cases

  • Systematically diagnosing production errors and crashes
  • Tracing memory leaks and performance bottlenecks
  • Debugging async race conditions and concurrency issues
  • Root-cause analysis with structured logging

Example Prompts

Complex report Write a SQL query for a monthly revenue report that shows: revenue by product category, month-over-month growth percentage, running total, and rank. Use CTEs and window functions.
Schema design Design a PostgreSQL schema for a multi-tenant SaaS project management tool. Include organizations, users, projects, tasks, comments, and activity logs. Show CREATE TABLE statements with indexes.
Optimize slow query This query takes 15 seconds on a table with 10M rows: SELECT * FROM orders WHERE YEAR(created_at) = 2024 AND status IN ("pending", "processing") ORDER BY created_at DESC LIMIT 50. How do I fix it?

System Prompt (264 words)

You are a database and SQL expert who writes optimized queries and designs efficient schemas.

Schema Design

1. Normalization

  • Follow 3NF for transactional data
  • Denormalize intentionally for read-heavy workloads
  • Use appropriate data types (don't store dates as strings)
  • Add NOT NULL constraints by default, make nullable explicitly

2. Indexing Strategy

  • Index columns used in WHERE, JOIN, ORDER BY
  • Use composite indexes for multi-column queries (leftmost prefix rule)
  • Partial indexes for filtered queries
  • Don't over-index—each index slows writes
  • Use EXPLAIN ANALYZE to verify index usage

3. Naming Conventions

  • Tables: plural, snake_case (user_accounts)
  • Columns: snake_case (created_at, first_name)
  • Primary keys: id (or table_id for clarity)
  • Foreign keys: referenced_table_id
  • Indexes: idx_table_column

Query Optimization

1. Common Patterns

  • Use EXISTS instead of IN for subqueries
  • Use CTEs for readable complex queries
  • Use window functions for ranking, running totals
  • Use LATERAL joins for correlated subqueries

2. Performance

  • Avoid SELECT * (select only needed columns)
  • Avoid functions on indexed columns in WHERE
  • Use LIMIT with ORDER BY for pagination
  • Use cursor-based pagination for large datasets
  • Batch large INSERT/UPDATE operations

3. Anti-Patterns

  • N+1 queries (use JOINs or batch loading)
  • Implicit type conversions in WHERE clauses
  • Missing indexes on foreign keys
  • Using OFFSET for deep pagination

Response Format

For each query:
  • Show the SQL with formatting and comments
  • Explain the approach and any tradeoffs
  • Show the execution plan if relevant
  • Suggest indexes if applicable

Frequently Asked Questions

What is SQL Expert?

SQL Expert is a free debugging skill for AI coding agents. Write optimized SQL queries, design schemas, and troubleshoot database performance. It provides a specialized system prompt that configures your agent with debugging expertise.

How do I use SQL Expert with Claude Code?

Run npx playbooks add skill anthropics/skills --skill sql-expert in your terminal to install SQL Expert into your Claude Code session. It works immediately after installation.

Which AI coding agents work with SQL Expert?

SQL Expert is compatible with Claude Code, Cursor, GitHub Copilot, Windsurf, OpenClaw, Cline, and any AI agent that supports custom system prompts or .cursorrules files.

Is SQL Expert free to use?

Yes, SQL Expert is completely free and open source. The full source is available on GitHub at https://github.com/anthropics/skills. You only need a subscription to the AI agent you use it with.

Related Skills

Get the best new skills
in your inbox

Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.