Assists with SQL query design, index strategy, and performance optimization for PostgreSQL, MySQL, SQL Server, and SQLite.
Install
mkdir -p .claude/skills/data-sql && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/11751" && unzip -o skill.zip -d .claude/skills/data-sql && rm skill.zipInstalls to .claude/skills/data-sql
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
SQL query design, optimization, EXPLAIN analysis, index strategy, pagination, upserts, and N+1 prevention for relational databases (PostgreSQL, MySQL, SQL Server, SQLite, Oracle). USE FOR: SQL queries, schema review, performance tuning, index strategy, query optimization, EXPLAIN plan analysis. DO NOT USE FOR: GraphQL APIs (data-graphql), MongoDB queries (data-mongodb), Redis caching (data-redis), full backend implementation (impl-* skills).Key capabilities
- →Design complex SQL queries including JOINs, CTEs, and window functions
- →Optimize existing SQL for correctness, performance, and security
- →Diagnose slow queries using EXPLAIN plans
- →Design and review index strategies
- →Fix N+1 query patterns in application code
- →Scan database schemas and onboard to databases
How it works
The skill detects the RDBMS, scans the schema, analyzes the request, then writes or optimizes SQL queries. It verifies performance with EXPLAIN plans and recommends indexes, producing an Implementation Complete Report.
Inputs & outputs
When to use data-sql
- →Optimizing slow database queries
- →Designing index strategies for tables
- →Writing complex SQL logic
- →Reviewing schema performance
About this skill
SQL Query Design and Optimization
When to Use
- Writing complex SQL queries (JOINs, CTEs, window functions, upserts).
- Reviewing existing SQL for correctness, performance, and security.
- Diagnosing slow queries via EXPLAIN plans.
- Designing or reviewing index strategy.
- Fixing N+1 query patterns in application code.
- Schema scanning and onboarding to a database.
When Not to Use
- GraphQL schema design or resolvers — use
data-graphql. - MongoDB queries or aggregation pipelines — use
data-mongodb. - Redis caching, rate limiting, or pub/sub — use
data-redis. - Full backend feature implementation — use
impl-python,impl-typescript-backend, or otherimpl-*skills. - Architecture or planning decisions — use
architecture-planning.
Procedure
- Detect database — Identify the RDBMS and version from project files (
package.json,prisma/schema.prisma,appsettings.json,settings.py,*.csproj, connection strings). Supported: PostgreSQL, MySQL/MariaDB, SQL Server, SQLite, Oracle. - Scan schema — If a database project or schema definition exists (ORM models, migration files,
.sqlproj), catalog tables, columns, types, constraints, indexes, and relationships. Produce a summary. - Analyze the request — Determine what is needed: write a query, review existing SQL, diagnose performance, or explain a schema.
- Write or optimize SQL — Produce correct, performant SQL following the standards below. Adapt syntax to the detected dialect.
- Verify with EXPLAIN — If executable, run
EXPLAIN ANALYZE(PostgreSQL) orEXPLAIN(MySQL) to validate the plan and interpret results. - Recommend indexes — Suggest indexes that would improve performance for the queries at hand.
- Produce the output contract — Write the Implementation Complete Report (see Output Contract below).
Standards
Database Coverage
Detect the target database from connection strings, ORM configuration, migration files, or database project files:
- PostgreSQL —
pg, Prisma withpostgresql, Djangopsycopg2, EF CoreNpgsql - MySQL / MariaDB —
mysql2, Prisma withmysql, Djangomysqlclient - SQL Server —
mssql,.sqlproj,.dacpac, EF CoreSqlServer - SQLite —
better-sqlite3, Prisma withsqlite, Djangosqlite3 - Oracle —
oracledb,cx_Oracle
Adapt syntax, functions, and optimization strategies to the detected dialect.
Schema Scanning Sources
ORM / Schema sources:
- Prisma:
schema.prisma— models, fields, relations, indexes, enums - TypeORM: entity decorators —
@Entity,@Column,@ManyToOne,@Index - Sequelize: model definitions —
define, associations - Django:
models.py—Modelclasses, fields,Meta,ForeignKey - EF Core:
DbContext, entity configurations, migrations - SQL Server projects:
.sqlproj,.sqltable/view/procedure definitions
Migration sources:
migrations/folders (Prisma, TypeORM, Sequelize, Django, Alembic, Flyway, Liquibase)- Raw
.sqlmigration files
Query Writing Patterns
SELECT with JOINs
SELECT
o.id AS order_id,
o.created_at,
c.name AS customer_name,
SUM(li.quantity * li.unit_price) AS order_total
FROM orders o
INNER JOIN customers c ON c.id = o.customer_id
INNER JOIN line_items li ON li.order_id = o.id
WHERE o.status = 'completed'
AND o.created_at >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY o.id, o.created_at, c.name
HAVING SUM(li.quantity * li.unit_price) > 100
ORDER BY order_total DESC;
CTEs and Window Functions
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', created_at) AS month,
SUM(total) AS revenue
FROM orders
WHERE status = 'completed'
GROUP BY DATE_TRUNC('month', created_at)
)
SELECT
month,
revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_month,
ROUND(
(revenue - LAG(revenue) OVER (ORDER BY month))
/ NULLIF(LAG(revenue) OVER (ORDER BY month), 0) * 100,
2
) AS growth_pct
FROM monthly_sales
ORDER BY month;
Upsert (PostgreSQL)
INSERT INTO inventory (product_id, warehouse_id, quantity)
VALUES ($1, $2, $3)
ON CONFLICT (product_id, warehouse_id)
DO UPDATE SET
quantity = inventory.quantity + EXCLUDED.quantity,
updated_at = NOW();
EXPLAIN / Performance Diagnosis
Always use EXPLAIN ANALYZE (PostgreSQL) or EXPLAIN (MySQL) to verify query performance:
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT ...;
Red flags in explain plans:
Seq Scanon large tables — missing indexNested Loopwith high row estimates — consider hash or merge joinSortwith high memory — add index to avoid sortingHash Joinwith spills to disk — increasework_memor reduce result setBitmap Heap Scanwith many recheck conditions — partial index may help
Common Bottlenecks and Fixes
| Bottleneck | Symptom | Fix |
|---|---|---|
| Missing index | Seq Scan on filtered column | CREATE INDEX on WHERE/JOIN columns |
| N+1 queries | Many small queries in a loop | Use JOINs or batch queries |
| Full table scan | No index used despite WHERE clause | Check column types match, add composite index |
| Lock contention | Queries waiting on locks | Reduce transaction scope, use SKIP LOCKED |
| Large result sets | Slow response, high memory | Add pagination (LIMIT/OFFSET or cursor) |
| Unoptimized subquery | Correlated subquery re-executes per row | Rewrite as JOIN or CTE |
| Missing statistics | Planner chooses bad plan | Run ANALYZE on the table |
Index Strategy
- Index columns used in WHERE, JOIN ON, ORDER BY, GROUP BY.
- Use composite indexes for multi-column filters (leftmost prefix rule).
- Consider partial indexes for filtered subsets (
WHERE active = true). - Use covering indexes (INCLUDE) to avoid heap lookups.
- Avoid over-indexing — each index slows writes.
N+1 Prevention
- Identify loops that issue one query per iteration.
- Replace with JOINs, subqueries, or batch
IN (...)queries. - When using ORMs, use eager loading (
include,prefetch_related,Include()).
Pagination
- Offset-based:
LIMIT $1 OFFSET $2— simple but degrades on deep pages. - Cursor-based:
WHERE id > $cursor ORDER BY id LIMIT $1— stable performance at any depth. - Always pair pagination with a deterministic ORDER BY.
Transaction Scope
- Keep transactions as short as possible.
- Use appropriate isolation levels (READ COMMITTED for most OLTP, SERIALIZABLE only when required).
- Use
SKIP LOCKEDfor queue-style processing to avoid contention.
SQL Injection Prevention
- Always use parameterized queries (
$1,?,@param). Never concatenate user input into SQL strings. - Validate and sanitize inputs at the application layer before they reach the query.
NULL Handling
- Use
COALESCEorNULLIFfor safe NULL comparisons. - Remember:
NULL != NULL— useIS NULL/IS NOT NULL. - Aggregations ignore NULLs — use
COALESCEin SUM/AVG when zero-default is needed. - Be cautious with
NOT INcontaining NULLs — preferNOT EXISTS.
Query Review Checklist
- Correctness — Does the query return the right results? Check JOIN conditions, WHERE filters, GROUP BY columns
- SQL injection — Are user inputs parameterized? Never concatenate strings into queries
- Performance — Are appropriate indexes used? Run EXPLAIN to verify
- N+1 patterns — Is the query called in a loop? Batch or join instead
- NULL handling — Are NULLs handled correctly in comparisons and aggregations?
- Data types — Do comparisons use matching types? Implicit casts defeat indexes
- Transaction scope — Is the transaction as short as possible? Are isolation levels appropriate?
- Idempotency — Can the query be safely retried? Use upserts where appropriate
Output Contract
All skills in the implementation phase family use this identical report. Present it in chat before logging progress.
### Implementation Complete Report
**Implementation summary**
[2-4 sentences: what was delivered and how it matches the request.]
**Scope**
- In scope: [bullets or "As specified in task"]
- Out of scope / deferred: [bullets or "None"]
**Acceptance criteria mapping**
| AC / criterion | Evidence |
|----------------|----------|
| [AC-1 or description] | [file path, test name, or behavior] |
_Use `N/A — [reason]` if no formal AC list exists._
**Changes**
| Path | Purpose |
|------|---------|
| `path/to/file` | [one line] |
**Verification**
- [command] — [result: pass/fail/skip]
- _If not run, state why._
**Risks and follow-ups**
- [concrete items] or **None**
**Suggested next step**
[Handoff target agent name or human action.]
Guardrails
- Adapt all SQL syntax to the detected database dialect. Do not assume PostgreSQL when the project uses MySQL or SQL Server.
- Do not introduce schema changes unless explicitly requested — focus on queries and indexes.
- Do not speculate on missing schema; scan the project or ask for clarification.
- Use
data-graphqlwhen the task involves GraphQL schema or resolvers. - Use
data-mongodbwhen the task involves MongoDB queries or aggregation. - Use
data-rediswhen the task involves Redis caching or data structures. - Use
impl-*skills when the task requires full backend feature implementation beyond SQL.
When not to use it
- →For GraphQL schema design or resolvers
- →For MongoDB queries or aggregation pipelines
- →For Redis caching, rate limiting, or pub/sub
Limitations
- →The skill adapts syntax to the detected database dialect.
- →It does not introduce schema changes unless explicitly requested.
- →It does not speculate on missing schema.
How it compares
This skill provides complete SQL query design and optimization, including EXPLAIN plan analysis and N+1 pattern prevention, unlike simply generating basic SQL statements.
Compared to similar skills
data-sql side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| data-sql (this skill) | 0 | 3mo | No flags | Intermediate |
| whodb | 1 | 4mo | Review | Beginner |
| database-schema-design | 0 | 3mo | No flags | Intermediate |
| database-design | 6 | 6mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
whodb
clidey
Database operations including querying, schema exploration, and data analysis. Activates for tasks involving PostgreSQL, MySQL, MariaDB, SQLite, MongoDB, Redis, Elasticsearch, or ClickHouse databases.
database-schema-design
RepairYourTech
Design database schemas with normalization, relationships, and constraints. Use when creating new database schemas, designing tables, or planning data models for any database paradigm.
database-design
davila7
Database design principles and decision-making. Schema design, indexing strategy, ORM selection, serverless databases.
vector-database-engineer
sickn33
Expert in vector databases, embedding strategies, and semantic search implementation. Masters Pinecone, Weaviate, Qdrant, Milvus, and pgvector for RAG applications, recommendation systems, and similar
sqlmap-database-penetration-testing
davila7
This skill should be used when the user asks to "automate SQL injection testing," "enumerate database structure," "extract database credentials using sqlmap," "dump tables and columns from a vulnerable database," or "perform automated database penetration testing." It provides comprehensive guidance for using SQLMap to detect and exploit SQL injection vulnerabilities.
generating-database-seed-data
jeremylongshore
Process this skill enables AI assistant to generate realistic test data and database seed scripts for development and testing environments. it uses faker libraries to create realistic data, maintains relational integrity, and allows configurable data volumes. u... Use when working with databases or data models. Trigger with phrases like 'database', 'query', or 'schema'.