mcpField
S
sql-ops

sql-ops

github.com/0xdarkmatterData & databases7838 installsUpdated 2w ago
Repository

About sql-ops

sql-ops is a quick-reference skill for people working with SQL, covering query structures, indexes, and transactions. It also lists common anti-patterns and suggested fixes.

Key features

  1. 01CTE and chained-query examples
  2. 02Window function reference
  3. 03JOIN type reference
  4. 04Pagination and isolation guidance
  5. 05Index types and anti-pattern fixes

Use cases

  1. 01Writing multi-step queries
  2. 02Analyzing ranks and running totals
  3. 03Choosing pagination or index approaches
  4. 04Reviewing common SQL mistakes

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/0xdarkmatter/claude-mods --skill sql-ops

After installing, type/sql-opsin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

assets/
.gitkeep
references/
indexing-strategies.md
window-functions.md
scripts/
.gitkeep

Details

From the project README

SQL Patterns

Quick reference for common SQL patterns.

CTE (Common Table Expressions)

WITH active_users AS (
    SELECT id, name, email
    FROM users
    WHERE status = 'active'
)
SELECT * FROM active_users WHERE created_at > '2024-01-01';
Chained CTEs
WITH
    active_users AS (
        SELECT id, name FROM users WHERE status = 'active'
    ),
    user_orders AS (
        SELECT user_id, COUNT(*) as order_count
        FROM orders GROUP BY user_id
    )
SELECT u.name, COALESCE(o.order_count, 0) as orders
FROM active_users u
LEFT JOIN user_orders o ON u.id = o.user_id;

Window Functions (Quick Reference)

FunctionUse
ROW_NUMBER()Unique sequential numbering
RANK()Rank with gaps (1, 2, 2, 4)
DENSE_RANK()Rank without gaps (1, 2, 2, 3)
LAG(col, n)Previous row value
LEAD(col, n)Next row value
SUM() OVERRunning total
AVG() OVERMoving average
SELECT
    date,
    revenue,
    LAG(revenue, 1) OVER (ORDER BY date) as prev_day,
    SUM(revenue) OVER (ORDER BY date) as running_total
FROM daily_sales;

JOIN Reference

TypeReturns
INNER JOINOnly matching rows
LEFT JOINAll left + matching right
RIGHT JOINAll right + matching left
FULL JOINAll rows, NULL where no match

Pagination

-- OFFSET/LIMIT (simple, slow for large offsets)
SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 40;

-- Keyset (fast, scalable)
SELECT * FROM products WHERE id > 42 ORDER BY id LIMIT 20;

Transaction Isolation Levels

LevelDirty ReadNon-Repeatable ReadPhantom ReadUse
READ UNCOMMITTEDPossiblePossiblePossibleRarely (PostgreSQL treats as READ COMMITTED)
READ COMMITTEDNoPossiblePossibleDefault in most databases
REPEATABLE READNoNoPossible*Consistent multi-statement reads
SERIALIZABLENoNoNoCritical invariants (retry on serialization failure)

*PostgreSQL's REPEATABLE READ also prevents phantoms via snapshot isolation.

BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- ... critical operations ...
COMMIT;  -- be prepared to retry on serialization failure

Keep the default READ COMMITTED globally; raise the level per-transaction only where the logic requires it.

Index Quick Reference

Index TypeBest For
B-treeRange queries, ORDER BY
HashExact equality only
GINArrays, JSONB, full-text
CoveringAvoid table lookup

Anti-Patterns

MistakeFix
SELECT *List columns explicitly
WHERE YEAR(date) = 2024WHERE date >= '2024-01-01'
NOT IN with NULLsUse NOT EXISTS
N+1 queriesUse JOIN or batch

Additional Resources

For detailed patterns, load:

  • ./references/window-functions.md - Complete window function patterns
  • ./references/indexing-strategies.md - Index types, covering indexes, optimization

Comments

Markdown supported