mcpField
S
sql

sql

github.com/htlin222Data & databases7838 installsUpdated 3w ago
Repository

About sql

sql is a skill for writing SQL queries and designing schemas. It helps with complex queries, performance analysis, index strategy, and migration planning.

Key features

  1. 01Write window and recursive queries
  2. 02Build queries with CTEs
  3. 03Analyze query plans
  4. 04Plan index strategies
  5. 05Design normalized schemas
  6. 06Plan database migrations

Use cases

  1. 01Optimizing a slow query
  2. 02Finding high-revenue customers
  3. 03Designing a database schema

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/htlin222/dotfiles --skill sql

After installing, type/sqlin Claude Code to confirm it loaded.

Details

From the project README

SQL Development

Write efficient SQL queries and design schemas.

When to Use

  • Writing complex queries
  • Query optimization
  • Schema design
  • Index strategy
  • Migration planning

Query Patterns

Window Functions
-- Running totals
SELECT
    date,
    amount,
    SUM(amount) OVER (ORDER BY date) as running_total,
    AVG(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as moving_avg_7d
FROM transactions;

-- Ranking
SELECT
    name,
    score,
    RANK() OVER (ORDER BY score DESC) as rank,
    DENSE_RANK() OVER (ORDER BY score DESC) as dense_rank,
    ROW_NUMBER() OVER (ORDER BY score DESC) as row_num
FROM players;

-- Partition by category
SELECT
    category,
    product,
    sales,
    sales * 100.0 / SUM(sales) OVER (PARTITION BY category) as pct_of_category
FROM products;
CTEs (Common Table Expressions)
WITH
monthly_sales AS (
    SELECT
        DATE_TRUNC('month', order_date) as month,
        SUM(amount) as total
    FROM orders
    GROUP BY 1
),
growth AS (
    SELECT
        month,
        total,
        LAG(total) OVER (ORDER BY month) as prev_month,
        (total - LAG(total) OVER (ORDER BY month)) / NULLIF(LAG(total) OVER (ORDER BY month), 0) * 100 as growth_pct
    FROM monthly_sales
)
SELECT * FROM growth WHERE growth_pct < 0;
Recursive CTEs
-- Hierarchical data (org chart, categories)
WITH RECURSIVE subordinates AS (
    SELECT id, name, manager_id, 1 as level
    FROM employees
    WHERE manager_id IS NULL

    UNION ALL

    SELECT e.id, e.name, e.manager_id, s.level + 1
    FROM employees e
    JOIN subordinates s ON e.manager_id = s.id
)
SELECT * FROM subordinates ORDER BY level, name;

Query Optimization

Index Strategy
-- Composite index for common queries
CREATE INDEX idx_orders_user_date ON orders(user_id, order_date DESC);

-- Partial index for filtered queries
CREATE INDEX idx_active_users ON users(email) WHERE status = 'active';

-- Check query plan
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123;
Common Issues
ProblemSymptomSolution
Missing indexSeq ScanAdd appropriate index
N+1 queriesMany small hitsUse JOIN or batch
SELECT *Slow + memorySelect only needed columns
No LIMITLarge resultAdd pagination
Function on colIndex not usedRewrite condition

Schema Design

-- Normalized schema
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES users(id),
    total DECIMAL(10,2) NOT NULL,
    status VARCHAR(20) DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE INDEX idx_orders_user ON orders(user_id);
CREATE INDEX idx_orders_status ON orders(status) WHERE status != 'completed';

Examples

Input: "Optimize this slow query" Action: Run EXPLAIN, identify bottlenecks, add indexes or rewrite query

Input: "Get top 10 customers by revenue" Action: Write aggregation with proper joins, ordering, and limit

Comments

Markdown supported