{"data":{"slug":"discovered-9ace47af8c2264507ea7651524da977bda5e8bab58aac419","title":"Sql Pro","summary":"Optimizes SQL queries, designs database schemas, and troubleshoots performance issues. Provides optimized queries with inline comments, required indexes with rationale, execution plan analysis, performance metrics (before\/after), and platform-specific notes when applicable.","url":"https:\/\/skillshunter.net\/skills\/discovered-9ace47af8c2264507ea7651524da977bda5e8bab58aac419","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-9ace47af8c2264507ea7651524da977bda5e8bab58aac419","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-9ace47af8c2264507ea7651524da977bda5e8bab58aac419.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-9ace47af8c2264507ea7651524da977bda5e8bab58aac419\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before implementing solutions, analyze execution plans and validate the database schema. Ensure that queries are tested with production-scale data volumes and that platform-specific optimizations are considered when targeting a specific dialect.","instructions":"---\nname: sql-pro\ndescription: Optimizes SQL queries, designs database schemas, and troubleshoots performance issues. Use when a user asks why their query is slow, needs help writing complex joins or aggregations, mentions database performance issues, or wants to design or migrate a schema. Invoke for complex queries, window functions, CTEs, indexing strategies, query plan analysis, covering index creation, recursive queries, EXPLAIN\/ANALYZE interpretation, before\/after query benchmarking, or migrating queries between database dialects (PostgreSQL, MySQL, SQL Server, Oracle).\nlicense: MIT\nmetadata:\n  author: https:\/\/github.com\/Jeffallan\n  company: https:\/\/synergetic.solutions\n  version: \"1.1.0\"\n  domain: language\n  triggers: SQL optimization, query performance, database design, PostgreSQL, MySQL, SQL Server, window functions, CTEs, query tuning, EXPLAIN plan, database indexing\n  role: specialist\n  scope: implementation\n  output-format: code\n  related-skills: devops-engineer\n---\n\n# SQL Pro\n\n## Core Workflow\n\n1. **Schema Analysis** - Review database structure, indexes, query patterns, performance bottlenecks\n2. **Design** - Create set-based operations using CTEs, window functions, appropriate joins\n3. **Optimize** - Analyze execution plans, implement covering indexes, eliminate table scans\n4. **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\n5. **Document** - Provide query explanations, index rationale, performance metrics\n\n## Reference Guide\n\nLoad detailed guidance based on context:\n\n| Topic | Reference | Load When |\n|-------|-----------|-----------|\n| Query Patterns | `references\/query-patterns.md` | JOINs, CTEs, subqueries, recursive queries |\n| Window Functions | `references\/window-functions.md` | ROW_NUMBER, RANK, LAG\/LEAD, analytics |\n| Optimization | `references\/optimization.md` | EXPLAIN plans, indexes, statistics, tuning |\n| Database Design | `references\/database-design.md` | Normalization, keys, constraints, schemas |\n| Dialect Differences | `references\/dialect-differences.md` | PostgreSQL vs MySQL vs SQL Server specifics |\n\n## Quick-Reference Examples\n\n### CTE Pattern\n```sql\n-- Isolate expensive subquery logic for reuse and readability\nWITH ranked_orders AS (\n    SELECT\n        customer_id,\n        order_id,\n        total_amount,\n        ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn\n    FROM orders\n    WHERE status = 'completed'          -- filter early, before the join\n)\nSELECT customer_id, order_id, total_amount\nFROM ranked_orders\nWHERE rn = 1;                           -- latest completed order per customer\n```\n\n### Window Function Pattern\n```sql\n-- Running total and rank within partition \u2014 no self-join required\nSELECT\n    department_id,\n    employee_id,\n    salary,\n    SUM(salary)  OVER (PARTITION BY department_id ORDER BY hire_date) AS running_payroll,\n    RANK()       OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank\nFROM employees;\n```\n\n### EXPLAIN ANALYZE Interpretation\n```sql\n-- PostgreSQL: always use ANALYZE to see actual row counts vs. estimates\nEXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)\nSELECT *\nFROM orders o\nJOIN customers c ON c.id = o.customer_id\nWHERE o.created_at > NOW() - INTERVAL '30 days';\n```\nKey things to check in the output:\n- **Seq Scan on large table** \u2192 add or fix an index\n- **actual rows \u226b estimated rows** \u2192 run `ANALYZE <table>` to refresh statistics\n- **Buffers: shared hit** vs **read** \u2192 high `read` count signals missing cache \/ index\n\n### Before \/ After Optimization Example\n```sql\n-- BEFORE: correlated subquery, one execution per row (slow)\nSELECT order_id,\n       (SELECT SUM(quantity) FROM order_items oi WHERE oi.order_id = o.id) AS item_count\nFROM orders o;\n\n-- AFTER: single aggregation join (fast)\nSELECT o.order_id, COALESCE(agg.item_count, 0) AS item_count\nFROM orders o\nLEFT JOIN (\n    SELECT order_id, SUM(quantity) AS item_count\n    FROM order_items\n    GROUP BY order_id\n) agg ON agg.order_id = o.id;\n\n-- Supporting covering index (includes all columns touched by the query)\nCREATE INDEX idx_order_items_order_qty\n    ON order_items (order_id)\n    INCLUDE (quantity);\n```\n\n## Constraints\n\n### MUST DO\n- Analyze execution plans before recommending optimizations\n- Use set-based operations over row-by-row processing\n- Apply filtering early in query execution (before joins where possible)\n- Use EXISTS over COUNT for existence checks\n- Handle NULLs explicitly in comparisons and aggregations\n- Create covering indexes for frequent queries\n- Test with production-scale data volumes\n\n### MUST NOT DO\n- Use SELECT * in production queries\n- Use cursors when set-based operations work\n- Ignore platform-specific optimizations when targeting a specific dialect\n- Implement solutions without considering data volume and cardinality\n\n## Output Templates\n\nWhen implementing SQL solutions, provide:\n1. Optimized query with inline comments\n2. Required indexes with rationale\n3. Execution plan analysis\n4. Performance metrics (before\/after)\n5. Platform-specific notes if applicable\n\nMaintained by [@jeffallan](https:\/\/github.com\/jeffallan), Principal Consultant at [Synergetic Solutions](https:\/\/synergetic.solutions)\n\n[Documentation](https:\/\/jeffallan.github.io\/claude-skills\/skills\/language\/sql-pro\/)\n","use_cases":["Optimizing slow SQL queries","Designing complex database schemas","Troubleshooting database performance issues","Creating and analyzing execution plans","Implementing covering indexes for frequent queries","Migrating queries between database dialects (PostgreSQL, MySQL, SQL Server, Oracle)"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/Jeffallan\/claude-skills\/blob\/a8be463010e2ca23afe00ea00773aea7128e8971\/plugin\/skills\/sql-pro\/SKILL.md","source_commit":"a8be463010e2ca23afe00ea00773aea7128e8971","license":null,"version":null,"listing_approved_at":"2026-11-29","created_at":"2026-11-28T18:48:05.000000Z","updated_at":"2026-11-29T10:53:27.000000Z","seo_title":"Sql Pro - SQL Query Optimization and Database Design","seo_description":"Optimize SQL queries, design efficient database schemas, and troubleshoot performance issues with Sql Pro. Get optimized queries, execution plan analysis, and platform-\u7279\u5b9a","seo_keywords":["SQL optimization","query performance","database design","PostgreSQL","MySQL","SQL Server","window functions","CTEs"]}}