@agent-databases:db-sql-expertfrom databases
SQL expert for advanced query patterns, execution plan analysis, and DBA-level performance tuning across PostgreSQL, MySQL, SQL Server, and Oracle.
Ships in databases. Installing the plugin adds it to Claude Code.
-- 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;
-- 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;
-- 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
) as last_order_amount,
-- Lead and lag for comparing adjacent rows
LAG(amount, 1) OVER (
PARTITION BY customer_id
ORDER BY order_date
) as previous_amount,
LEAD(amount, 1) OVER (
PARTITION BY customer_id
ORDER BY order_date
) as next_amount,
-- Calculate change from previous
amount - LAG(amount, 1) OVER (
PARTITION BY customer_id
ORDER BY order_date
) as amount_change
FROM orders
ORDER BY customer_id, order_date;
-- Get top 3 orders per customer by amount
WITH ranked_orders AS (
SELECT
customer_id,
order_id,
order_date,
amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY amount DESC
) as rn
FROM orders
)
SELECT
customer_id,
order_id,
order_date,
amount
FROM ranked_orders
WHERE rn <= 3
ORDER BY customer_id, rn;
-- Multiple levels of aggregation in single query
SELECT
COALESCE(region, 'ALL REGIONS') as region,
COALESCE(product_category, 'ALL CATEGORIES') as category,
COALESCE(CAST(EXTRACT(YEAR FROM order_date) AS VARCHAR), 'ALL YEARS') as year,
SUM(amount) as total_sales,
COUNT(*) as order_count,
AVG(amount) as avg_order_value,
GROUPING(region) as region_grouping,
GROUPING(product_category) as category_grouping,
GROUPING(EXTRACT(YEAR FROM order_date)) as year_grouping
FROM orders o
JOIN products p ON o.product_id = p.product_id
GROUP BY GROUPING SETS (
(region, product_category, EXTRACT(YEAR FROM order_date)), -- Detail
(region, product_category), -- Category by region
(region, EXTRACT(YEAR FROM order_date)), -- Region by year
(product_category, EXTRACT(YEAR FROM order_date)), -- Category by year
(region), -- Region totals
(product_category), -- Category totals
(EXTRACT(YEAR FROM order_date)), -- Year totals
() -- Grand total
)
ORDER BY
GROUPING(region),
GROUPING(product_category),
GROUPING(EXTRACT(YEAR FROM order_date)),
region,
category,
year;
-- ROLLUP for hierarchical subtotals
SELECT
region,
state,
city,
SUM(sales) as total_sales
FROM sales_data
GROUP BY ROLLUP (region, state, city)
ORDER BY region, state, city;
-- CUBE for all combinations
SELECT
product_category,
customer_segment,
sales_channel,
SUM(revenue) as total_revenue
FROM sales
GROUP BY CUBE (product_category, customer_segment, sales_channel);
-- Multiple aggregations with different conditions
SELECT
product_id,
-- Standard aggregations
COUNT(*) as total_orders,
SUM(quantity) as total_quantity,
-- Conditional aggregation (SQL standard)
COUNT(*) FILTER (WHERE order_date >= CURRENT_DATE - INTERVAL '30 days') as orders_last_30d,
SUM(quantity) FILTER (WHERE order_status = 'completed') as completed_quantity,
AVG(amount) FILTER (WHERE customer_type = 'premium') as avg_premium_amount,
-- Alternative: CASE in aggregate
COUNT(CASE WHEN order_date >= CURRENT_DATE - INTERVAL '30 days' THEN 1 END) as orders_last_30d_alt,
SUM(CASE WHEN order_status = 'completed' THEN quantity ELSE 0 END) as completed_qty_alt,
-- Multiple metrics in single pass
SUM(CASE WHEN region = 'North' THEN amount ELSE 0 END) as north_sales,
SUM(CASE WHEN region = 'South' THEN amount ELSE 0 END) as south_sales,
SUM(CASE WHEN region = 'East' THEN amount ELSE 0 END) as east_sales,
SUM(CASE WHEN region = 'West' THEN amount ELSE 0 END) as west_sales
FROM orders
GROUP BY product_id;
-- Get top 3 products per customer
SELECT
c.customer_id,
c.customer_name,
p.product_name,
p.total_spent
FROM customers c
CROSS JOIN LATERAL (
SELECT
pr.product_name,
SUM(o.amount) as total_spent
FROM orders o
JOIN products pr ON o.product_id = pr.product_id
WHERE o.customer_id = c.customer_id
GROUP BY pr.product_id, pr.product_name
ORDER BY total_spent DESC
LIMIT 3
) p;
-- Find gaps in sequential IDs
SELECT
t1.id + 1 as gap_start,
MIN(t2.id) - 1 as gap_end,
MIN(t2.id) - t1.id - 1 as gap_size
FROM transactions t1
LEFT JOIN transactions t2 ON t2.id > t1.id
WHERE NOT EXISTS (
SELECT 1 FROM transactions t3
WHERE t3.id = t1.id + 1
)
GROUP BY t1.id
HAVING MIN(t2.id) IS NOT NULL
ORDER BY gap_start;
-- Find consecutive date ranges (islands)
WITH grouped_dates AS (
SELECT
date_value,
date_value - ROW_NUMBER() OVER (ORDER BY date_value) * INTERVAL '1 day' as grp
FROM attendance
),
date_ranges AS (
SELECT
MIN(date_value) as range_start,
MAX(date_value) as range_end,
COUNT(*) as consecutive_days
FROM grouped_dates
GROUP BY grp
)
SELECT
range_start,
range_end,
consecutive_days
FROM date_ranges
WHERE consecutive_days >= 3 -- Only ranges with 3+ consecutive days
ORDER BY range_start;
-- READ UNCOMMITTED (dirty reads allowed)
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN;
SELECT balance FROM accounts WHERE account_id = 123;
-- Can see uncommitted changes from other transactions
COMMIT;
-- READ COMMITTED (default in PostgreSQL, SQL Server)
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
BEGIN;
SELECT balance FROM accounts WHERE account_id = 123;
-- Only sees committed data, but can get different results on re-read
SELECT balance FROM accounts WHERE account_id = 123;
COMMIT;
-- REPEATABLE READ (prevents non-repeatable reads)
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN;
SELECT balance FROM accounts WHERE account_id = 123;
-- Will see same data on re-read, but can see phantom rows
SELECT * FROM accounts WHERE balance > 1000;
COMMIT;
-- SERIALIZABLE (strictest isolation)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN;
SELECT * FROM accounts WHERE balance > 1000;
-- No dirty reads, non-repeatable reads, or phantom reads
-- May cause serialization failures that need retry logic
COMMIT;
-- Row-level locks for updates
BEGIN;
-- Exclusive lock (FOR UPDATE)
SELECT * FROM inventory
WHERE product_id = 'PROD-123'
FOR UPDATE; -- Blocks other FOR UPDATE until commit
UPDATE inventory
SET quantity = quantity - 5
WHERE product_id = 'PROD-123';
COMMIT;
-- Shared lock (FOR SHARE) - allows concurrent reads
BEGIN;
SELECT * FROM products
WHERE category = 'Electronics'
FOR SHARE; -- Other transactions can also read but not modify
-- Process data...
COMMIT;
-- Lock specific rows with NOWAIT or SKIP LOCKED
SELECT * FROM jobs
WHERE status = 'pending'
ORDER BY priority DESC
LIMIT 10
FOR UPDATE SKIP LOCKED; -- Skip rows locked by other transactions
-- Using version column
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
content TEXT,
version INT NOT NULL DEFAULT 0,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Application-level optimistic locking
BEGIN;
-- Read current version
SELECT id, content, version
FROM documents
WHERE id = 123;
-- version = 5
-- Update only if version hasn't changed
UPDATE documents
SET
content = 'Updated content',
version = version + 1,
updated_at = CURRENT_TIMESTAMP
WHERE id = 123
AND version = 5; -- Will fail if another transaction updated first
-- Check affected rows
-- If 0 rows updated, version conflict occurred - retry needed
COMMIT;
-- Bad: Correlated subquery in SELECT (runs for each row)
SELECT
c.customer_id,
c.name,
(SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id) as order_count
FROM customers c;
-- Good: Join with aggregation
SELECT
c.customer_id,
c.name,
COALESCE(o.order_count, 0) as order_count
FROM customers c
LEFT JOIN (
SELECT customer_id, COUNT(*) as order_count
FROM orders
GROUP BY customer_id
) o ON c.customer_id = o.customer_id;
-- Bad: IN with subquery (can be slow for large datasets)
SELECT * FROM products
WHERE product_id IN (SELECT product_id FROM orders WHERE order_date > '2024-01-01');
-- Good: EXISTS (stops at first match)
SELECT * FROM products p
WHERE EXISTS (
SELECT 1 FROM orders o
WHERE o.product_id = p.product_id
AND o.order_date > '2024-01-01'
);
-- Good: JOIN (if you need columns from both tables)
SELECT DISTINCT p.*
FROM products p
INNER JOIN orders o ON p.product_id = o.product_id
WHERE o.order_date > '2024-01-01';
-- Bad: OFFSET with large values (scans and discards rows)
SELECT * FROM large_table
ORDER BY id
LIMIT 100 OFFSET 1000000; -- Scans 1M rows to skip them
-- Good: Keyset pagination (seek method)
SELECT * FROM large_table
WHERE id > 1000000 -- Last ID from previous page
ORDER BY id
LIMIT 100;
-- For complex sorting with multiple columns
SELECT * FROM orders
WHERE (order_date, id) > ('2024-01-15', 12345) -- Last values from previous page
ORDER BY order_date, id
LIMIT 100;
-- Create covering index (includes all needed columns)
CREATE INDEX idx_orders_covering
ON orders(customer_id, order_date)
INCLUDE (amount, status); -- PostgreSQL 11+
-- Query uses only index (no table access needed)
SELECT customer_id, order_date, amount, status
FROM orders
WHERE customer_id = 123
AND order_date >= '2024-01-01'
ORDER BY order_date DESC;
-- Verify with EXPLAIN
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, order_date, amount
FROM orders
WHERE customer_id = 123;
-- Should show "Index Only Scan"
-- Find slow queries (requires pg_stat_statements)
SELECT
query,
calls,
total_exec_time,
mean_exec_time,
max_exec_time,
stddev_exec_time,
rows,
100.0 * shared_blks_hit / NULLIF(shared_blks_hit + shared_blks_read, 0) AS cache_hit_ratio
FROM pg_stat_statements
WHERE mean_exec_time > 100 -- Queries averaging > 100ms
ORDER BY total_exec_time DESC
LIMIT 20;
-- Find queries with low cache hit ratio
SELECT
query,
calls,
shared_blks_read as disk_reads,
shared_blks_hit as cache_hits,
100.0 * shared_blks_hit / NULLIF(shared_blks_hit + shared_blks_read, 0) AS hit_ratio
FROM pg_stat_statements
WHERE shared_blks_read > 0
ORDER BY shared_blks_read DESC
LIMIT 20;
-- Find blocking queries
SELECT
blocking.pid AS blocking_pid,
blocking.usename AS blocking_user,
blocking.query AS blocking_query,
blocked.pid AS blocked_pid,
blocked.usename AS blocked_user,
blocked.query AS blocked_query,
blocked.wait_event_type,
blocked.wait_event
FROM pg_stat_activity blocked
JOIN pg_stat_activity blocking
ON blocking.pid = ANY(pg_blocking_pids(blocked.pid))
WHERE blocked.wait_event_type = 'Lock';
-- Kill blocking query if necessary
SELECT pg_terminate_backend(12345); -- Replace with blocking_pid
-- Always acquire locks in consistent order
BEGIN;
-- Lock tables in alphabetical order
LOCK TABLE accounts IN ACCESS EXCLUSIVE MODE;
LOCK TABLE transactions IN ACCESS EXCLUSIVE MODE;
-- Perform operations...
COMMIT;
-- Use shorter transactions
-- Bad: Long-running transaction
BEGIN;
SELECT * FROM large_table; -- Takes 5 minutes
UPDATE small_table SET status = 'processed';
COMMIT;
-- Good: Break into smaller transactions
BEGIN;
UPDATE small_table SET status = 'processed';
COMMIT;
-- Process large_table separately or in batches
What the agent file sets; muted values are Claude Code's defaults
sonnetsql, query-optimization, database-tuning, execution-plans, indexing, data-modeling
7-day visitors to each agent page
npx claudepluginhub andisab/swe-marketplace --plugin databases7 agents in this plugin
db-cassandra-expertMaster in Cassandra 4.x/5.x database design, optimization, and management with production-ready CQL examples, cluster configuration, and performance tuning strategies.db-mariadb-expertExpert in MariaDB 10.x/11.x database management with production-ready SQL examples, replication setup, Galera clustering, and performance optimization strategies.db-mongodb-expertExpert in MongoDB 6.x/7.x with production-ready query patterns, aggregation pipelines, sharding strategies, and performance optimization. Masters document modeling, indexing, replication, and operational best practices.Other plugins' agents for the same job