Part of Data & Analytics

Claude Code Skills for SQL & Query

SQL is still the most important language in data work, and writing correct SQL is only half the job. Writing SQL that performs well on real datasets, that's readable by the next person, and that doesn't silently produce wrong results — that's the other half. These skills cover query writing, performance tuning, optimization, and the practical SQL work that data teams do every day.

Published by ClaudeVaultLast updated 3 skills

Key takeaway

ClaudeVault's SQL and query skills give Claude Code structured workflows for the three SQL disciplines that separate correct queries from fast, maintainable ones — query writing with CTEs, window functions, and dialect-aware syntax for PostgreSQL, MySQL, BigQuery, and Snowflake, performance tuning using EXPLAIN ANALYZE output to identify bottlenecks, and query optimization that restructures slow queries without changing their results. Claude Code connects directly to databases via DBHub MCP for schema-aware SQL generation from the terminal.

At a glance

  • 3 skills covering SQL query generation, query performance tuning with execution plan analysis, and structural query optimization for complex workloads
  • Claude Code connects directly to PostgreSQL, MySQL, SQL Server, and SQLite via DBHub MCP, enabling schema inspection and text-to-SQL generation from the terminal
  • AI SQL generation tools report approximately 90 percent accuracy on plain-English-to-SQL conversion across 10-plus SQL dialects in 2026
  • The key differentiator between AI SQL tools comes down to three factors: schema awareness, dialect accuracy, and workflow integration — generic text-to-SQL without schema context produces syntactically valid but semantically wrong queries

When you reach for these skills

  • When a dashboard query takes 45 seconds to load and nobody has looked at the execution plan to understand why

  • When analysts copy-paste SQL from Slack because writing correct joins across six normalized tables is error-prone without schema documentation

  • When query performance degrades as data volume grows and the fix has been adding more warehouse compute instead of optimizing the query itself

How these skills work together

A Claude Code SQL workflow starts with writing correct queries, then tunes them for performance using execution plan analysis, and finally restructures complex queries for long-term maintainability.

  1. 1

    Write the SQL query with schema awareness

    Start with the SQL writer. Claude generates queries with CTEs for readability, window functions where aggregation spans rows, and dialect-specific syntax for your target database — PostgreSQL array operations, BigQuery UNNEST patterns, Snowflake FLATTEN. With DBHub MCP connected, Claude reads the live schema so every table and column reference is validated against reality.

  2. 2

    Tune query performance using execution plan analysis

    The SQL performance tuner reads EXPLAIN ANALYZE output and identifies the specific bottleneck — sequential scans that should be index scans, hash joins that spill to disk, subqueries that execute per-row instead of once. Claude translates execution plan nodes into plain language and recommends the index, join rewrite, or materialization that fixes the actual bottleneck.

  3. 3

    Optimize complex queries for maintainability and speed

    Finally, the query optimizer restructures complex queries without changing their results. Claude replaces correlated subqueries with joins, materializes expensive CTEs used multiple times, rewrites OR chains as UNION ALL when the optimizer cannot merge them, and adds partition pruning hints for large time-series tables.

Outcome

Correct SQL generated with schema awareness and dialect-specific syntax, performance bottlenecks identified from execution plans, and complex queries restructured for both speed and readability — three skills that cover the full SQL lifecycle from writing to production tuning.

Compare the skills

SkillBest forComplexityPrimary use case
SQL WriterQuery generation and dialect-aware syntaxBeginnerCTEs, window functions, and joins with schema validation via MCP
SQL Performance TunerExecution plan analysis and index recommendationsAdvancedEXPLAIN ANALYZE interpretation with specific bottleneck diagnosis
Query OptimizerComplex query restructuringAdvancedSubquery elimination, materialization, and partition pruning for large datasets

Skills in this topic

SQL Performance Tuner

Diagnoses slow SQL queries through execution plan analysis with targeted index, rewrite, and configuration fixes. Use when a specific query runs too slowly and you have the execution plan. EXPLAIN ANALYZE interpretation, index strategy, query rewriting.

Diagnose why a specific SQL query is slow by reading its execution plan, identifying the highest-cost operations, and prescribing targeted fixes — indexes, rewrites, or configuration changes — with es

SQL Writer

Translates natural language data requests into correct, optimized SQL with proper joins, aggregation, and injection prevention. Use when you need a query written from a plain-English description. Dialect-aware, parameterized, safe queries.

Translate natural language data requests into correct, optimized, safe SQL — with proper joins, filtering, aggregation, and parameterized placeholders for application code.

Query Optimizer

Analyzes slow SQL queries through execution plans and recommends indexing, rewrites, and configuration changes. Use when SQL queries run too slowly. EXPLAIN analysis, index strategy, query rewriting, materialized views.

Analyze slow queries, interpret execution plans, identify bottlenecks, and recommend specific optimizations — rewrites, indexes, schema changes, or configuration adjustments — with estimated impact.

Frequently asked questions

Can Claude Code write SQL queries?

Yes. The SQL writer generates queries with CTEs, window functions, and dialect-specific syntax for PostgreSQL, MySQL, BigQuery, Snowflake, and SQL Server. Claude Code connects to databases via DBHub MCP, which means it reads the live schema and validates every table and column reference — producing queries that are not just syntactically valid but semantically correct.

How do I read EXPLAIN ANALYZE output?

The SQL performance tuner translates execution plan nodes into plain language. It identifies the specific bottleneck — a sequential scan that needs an index, a hash join spilling to disk, a nested loop executing per-row — and recommends the fix. Most slow queries have one or two root causes, not a dozen, and the execution plan pinpoints them.

When should I use CTEs vs subqueries?

CTEs are almost always more readable and maintainable. Performance is database-dependent: PostgreSQL materializes CTEs by default before version 12, which can hurt performance if the CTE result is large. BigQuery and Snowflake inline CTEs automatically. The SQL writer generates the appropriate pattern for your dialect.

How does Claude Code connect to my database?

Through DBHub MCP — a Model Context Protocol server that connects to PostgreSQL, MySQL, SQL Server, and SQLite. Once connected, Claude Code can inspect schemas, list tables and columns, and generate SQL that references real table structures instead of guessing. The connection uses read-only access by default for safety.

What SQL optimization techniques have the highest impact?

Three techniques cover most performance improvements: adding indexes on columns used in WHERE and JOIN clauses, replacing correlated subqueries with joins or lateral joins, and adding partition pruning conditions for time-series tables. The query optimizer identifies which of these applies to your specific slow query using the execution plan.