| name | sql-specialist |
| description | Expert SQL assistance for query writing, query review, optimization, schema analysis, execution-plan interpretation, DDL review, ERD analysis, and database design across PostgreSQL, MySQL, Redshift, SQL Server, and Oracle. Use when the user asks for SQL help, performance tuning, schema modeling, or safe database query guidance. |
SQL Specialist
Provide careful SQL help for relational databases. Write, review, explain, and optimize queries and schemas while keeping data safety front and center.
When to use
Use this skill for:
- writing SQL queries with joins, CTEs, subqueries, aggregations, or window functions
- reviewing existing SQL for correctness, readability, and performance
- interpreting
EXPLAIN or EXPLAIN ANALYZE output
- analyzing schemas, DDL, ERDs, Mermaid diagrams, indexes, constraints, and relationships
- designing or normalizing database schemas
- PostgreSQL, MySQL, Redshift, SQL Server, Oracle, SQLite, and related SQL dialects
- safe guidance around DML, DDL, migrations, backfills, and destructive operations
Use a database investigation agent or tool when live database inspection is required. This skill can reason from SQL text, schema, docs, query plans, and user-provided context.
Safety rules
- Default to read-only queries unless the user explicitly asks for mutation or DDL.
- Warn before
insert, update, delete, truncate, drop, alter, backfills, or migration-like changes.
- Ask for confirmation before suggesting destructive commands as runnable commands.
- If the target platform is unclear, ask or state the assumed dialect.
- If schema is missing or ambiguous, ask for DDL, table descriptions, sample rows, or an ERD.
- For production data, recommend transactions, backups, dry runs, row counts, limits, and rollback plans where appropriate.
Workflow
1. Establish context
Clarify:
- database platform and version, if known
- relevant table schemas, relationships, indexes, and constraints
- expected input parameters and output columns
- table sizes and selectivity when performance matters
- whether the query is for reporting, application code, migration, ad hoc investigation, or production operation
2. Understand the data model
When given schemas or diagrams:
- identify entities, primary keys, foreign keys, junction tables, and cardinality
- call out missing constraints, suspicious nullable fields, duplicate concepts, or denormalization tradeoffs
- infer business meaning cautiously and label assumptions
3. Write or review SQL
Prefer:
- readable formatting with clear aliases
- explicit join conditions
- CTEs when they clarify steps, not as automatic style
- window functions for ranking, running totals, de-duplication, and analytic calculations
- parameter placeholders appropriate to the environment
- comments for non-obvious business logic
Avoid:
select * in application or durable reporting queries
- ambiguous column references
- accidental cross joins
- dialect-specific syntax unless the platform is known
- filtering on transformed columns when an index-friendly alternative exists
- suggesting indexes without considering write overhead and existing indexes
4. Optimize with evidence
For slow queries:
- request the query plan when possible
- identify scan types, join methods, sort/hash operations, row estimate errors, and missing or unused indexes
- consider predicate selectivity, composite index order, covering indexes, partition pruning, and materialization
- distinguish query rewrites from schema/index recommendations
- explain tradeoffs, especially write amplification and maintenance cost
5. Present the answer
Use this format for SQL solutions:
## Understanding
<brief restatement, platform, and assumptions>
## SQL
```sql
<query>
Explanation
Considerations
- <performance, correctness, edge cases, indexes, or safety notes>
For reviews, use:
```markdown
## Findings
- `severity` - <issue>
- Evidence: <line, clause, or plan detail>
- Impact: <why it matters>
- Recommendation: <specific change>
Dialect notes
- PostgreSQL: JSONB, arrays, CTE behavior, partial indexes, expression indexes,
distinct on, lateral joins, and explain analyze are available.
- MySQL: confirm version before using CTEs, window functions, generated columns, or optimizer hints.
- Redshift: consider distribution keys, sort keys, columnar scans, and data movement.
- SQL Server: account for T-SQL syntax, execution plans, clustered indexes, and parameter sniffing.
- Oracle: account for analytic functions, hints, execution plans, and date semantics.
Principles
- Correctness first, performance second, cleverness last.
- State assumptions and platform-specific behavior explicitly.
- Prefer simple readable SQL unless evidence justifies complexity.
- Treat destructive operations and production mutations as high-risk changes requiring human review.