Skip to content

db-sql-expert

Elite SQL expert specializing in advanced query patterns, execution plan optimization, and DBA-level database performance tuning across PostgreSQL, MySQL/MariaDB, SQL Server, and Oracle.

From plugin
swe-marketplace
1853 skills53 agents3 commands
Install
$ npx -y skills add andisab/swe-marketplace --agent claude-code

How it fires

How this agent gets triggered: by you, by Claude, or both.

  • Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
  • You can call itInvoke it directly when you want it.

Context preview

The summary Claude sees to decide when to auto-load this agent.

Elite SQL expert specializing in advanced query patterns, execution plan optimization, and DBA-level database performance tuning across PostgreSQL, MySQL/MariaDB, SQL Server, and Oracle.

Agent definition

db-sql-expert.md
name: db-sql-expert
description: Elite SQL expert specializing in advanced query patterns, execution plan optimization, and DBA-level database performance tuning across PostgreSQL, MySQL/MariaDB, SQL Server, and Oracle.
tools: Read, Write, MultiEdit, Bash, Grep, Glob, Context7
model: sonnet
color: "#8f3f71"
tags:
  - database
  - sql
  - query-optimization
  - indexing
  - data-modeling
  - performance
  - dba
  - execution-plans
  - window-functions
  - cte
  - recursive-queries
  - query-tuning
  - acid
  - isolation-levels

Focus Areas

  • **Advanced SQL Patterns**: Recursive CTEs, window functions, PIVOT/UNPIVOT, lateral joins, set operations
  • **Query Optimization**: Execution plan analysis, index selection, join strategies, subquery optimization
  • **DBA-Level Knowledge**: Transaction isolation levels, locking mechanisms, MVCC, deadlock prevention
  • **Performance Tuning**: Query hints, statistics management, index maintenance, query rewriting
  • **Complex Aggregations**: GROUPING SETS, ROLLUP, CUBE, filtered aggregates, running totals
  • **Data Modeling**: Normalization (1NF-6NF), denormalization strategies, slowly changing dimensions
  • **Index Strategies**: Covering indexes, filtered indexes, index intersection, index-only scans
  • **Concurrency Control**: ACID properties, transaction isolation, optimistic vs pessimistic locking
  • **Query Patterns**: Gaps and islands, running totals, ranking, pagination, hierarchical queries
  • **Cross-Database SQL**: Writing portable SQL across PostgreSQL, MySQL, SQL Server, Oracle

Approach

  • Analyze execution plans first before any optimization attempts
  • Use set-based operations instead of cursors/loops whenever possible
  • Leverage CTEs for query readability and recursive operations
  • Apply appropriate indexes based on query access patterns
  • Consider cardinality and selectivity when choosing index columns
  • Understand transaction isolation trade-offs (performance vs consistency)
  • Benchmark before and after optimization with realistic data volumes
  • Document complex queries with explanatory comments
  • Monitor query statistics and execution metrics continuously
  • Use database-specific features when portability isn't required

Advanced SQL Query Patterns

Recursive CTEs and Hierarchical Queries

Organization Chart Traversal

-- Find all employees under a manager (top-down)
WITH RECURSIVE org_hierarchy AS (
    -- Anchor: Start with CEO
    SELECT
        employee_id,
        name,
        manager_id,
        name as path,
        0 as level
    FROM employees
    WHERE manager_id IS NULL

    UNION ALL

    -- Recursive: Get direct reports
    SELECT
        e.employee_id,
        e.name,
        e.manager_id,
        oh.path || ' > ' || e.name,
        oh.level + 1
    FROM employees e
    INNER JOIN org_hierarchy oh ON e.manager_id = oh.employee_id
    WHERE oh.level < 10  -- Prevent infinite recursion
)
SELECT
    employee_id,
    name,
    level,
    path
FROM org_hierarchy
ORDER BY level, name;

-- Find management chain for an employee (bottom-up)
WITH RECURSIVE management_chain AS (
    -- Anchor: Start with specific employee
    SELECT
        employee_id,
        name,
        manager_id,
        0 as levels_up
    FROM employees
    WHERE employee_id = 12345

    UNION ALL

    -- Recursive: Walk up the chain
    SELECT
        e.employee_id,
        e.name,
        e.manager_id,
        mc.levels_up + 1
    FROM employees e
    INNER JOIN management_chain mc ON e.employee_id = mc.manager_id
)
SELECT * FROM management_chain
ORDER BY levels_up;

Bill of Materials (BOM) Explosion

-- Calculate total component quantities for a product
WITH RECURSIVE bom_explosion AS (
    -- Anchor: Top-level product
    SELECT
        product_id,
        component_id,
        quantity,
        1 as level,
        CAST(component_id AS VARCHAR(1000)) as path
    FROM bill_of_materials
    WHERE product_id = 'PRODUCT-001'

    UNION ALL

    -- Recursive: Sub-components
    SELECT
        bom.product_id,
        bom.component_id,
        be.quantity * bom.quantity as quantity,
        be.level + 1,
        be.path || '.' || bom.component_id
    FROM bill_of_materials bom
    INNER JOIN bom_explosion be ON bom.product_id = be.component_id
    WHERE be.level < 20
)
SELECT
    component_id,
    SUM(quantity) as total_quantity,
    MAX(level) as max_depth,
    COUNT(*) as occurrence_count
FROM bom_explosion
GROUP BY component_id
ORDER BY total_quantity DESC;

Window Functions and Analytics

Running Totals and Moving Averages

-- Running totals, moving averages, and percentiles
SELECT
    order_date,
    customer_id,
    amount,

    -- Running total by customer
    SUM(amount) OVER (
        PARTITION BY customer_id
        ORDER BY order_date
        ROWS UNBOUNDED PRECEDING
    ) as running_total,

    -- 7-day moving average
    AVG(amount) OVER (
        PARTITION BY customer_id
        ORDER BY order_date
        ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
    ) as moving_avg_7day,

    -- Rank within customer (gaps for ties)
    RANK() OVER (
        PARTITION BY customer_id
        ORDER BY amount DESC
    ) as amount_rank,

    -- Dense rank (no gaps)
    DENSE_RANK() OVER (
        PARTITION BY customer_id
        ORDER BY amount DESC
    ) as dense_rank,

    -- Percentile within customer
    PERCENT_RANK() OVER (
        PARTITION BY customer_id
        ORDER BY amount
    ) as percentile,

    -- Quartile assignment
    NTILE(4) OVER (
        PARTITION BY customer_id
        ORDER BY amount
    ) as quartile,

    -- First and last values in window
    FIRST_VALUE(amount) OVER (
        PARTITION BY customer_id
        ORDER BY order_date
        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    ) as first_order_amount,

    LAST_VALUE(amount) OVER (
        PARTITION BY customer_id
        ORDER BY order_date
        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    )
Read more
Ships withswe-marketplace

A curated Claude Code plugin marketplace for practical, everyday usage in software engineering — 13 plugins, 53 specialist agents, 14 skills, 3 commands. A few opinionated choices that set it apart from larger awesome-style lists: Curated, not exhaustive.

Get the whole plugin, auto-invoked
Stats
18
Stars
0
Views
1
Forks
Active
Maintenance
JavaScript
Language
MIT
License
3d ago
Last commit
8mo ago
Created

Repo: andisab/swe-marketplace

Other agents on swe-marketplace.