Sql pro

From kumaran-is/claude-code-onboarding · View interactive version

Sql pro is an AI agent skill from the GitHub repository kumaran-is/claude-code-onboarding. Masters modern SQL across PostgreSQL, BigQuery, Snowflake, and hybrid OLTP/OLAP systems — covering advanced query techniques, dimensional modeling, time-series SQL, and data warehouse patterns. Use when writing complex analytics SQL, designing cloud database schemas, or optimizing cross-platform SQL

How to use this skill

Skill file

Iron Law

NEVER WRITE SQL WITHOUT KNOWING THE QUERY PLAN — USE EXPLAIN ANALYZE BEFORE CLAIMING A QUERY IS OPTIMIZED

When to Use This Skill

Do Not Use This Skill When

Quick Reference

| Workload | Platform | Key Consideration |

|----------|----------|-------------------|

| OLTP (transactional) | PostgreSQL | Normalize to 3NF, index FKs and WHERE cols |

| OLAP (analytics) | BigQuery / Snowflake / Redshift | Denormalize, columnar storage, partition by date |

| HTAP (both) | CockroachDB / TiDB | Read replicas for analytics, avoid read-your-own-writes latency |

| Time-series | TimescaleDB / InfluxDB | Hypertables, continuous aggregates, retention policies |

| Data warehouse | Redshift / Databricks | Star schema, materialized views, query concurrency |

| Graph + SQL | Neo4j / Amazon Neptune | Cypher for traversal, SQL for aggregation |

Process

Phase 1 — Understand the Workload

Phase 2 — Design the Query or Schema

Phase 3 — Optimize and Validate

Phase 4 — Production Readiness

Platform-Specific Capabilities

PostgreSQL (Primary Stack)

Cloud Analytics Platforms

Time-Series

Advanced SQL Patterns

Window Functions

-- Running total + rank within partition
SELECT
    user_id,
    order_date,
    total,
    SUM(total) OVER (PARTITION BY user_id ORDER BY order_date) AS running_total,
    RANK() OVER (PARTITION BY user_id ORDER BY total DESC) AS rank_in_user
FROM orders;

Recursive CTE (Hierarchical Data)

WITH RECURSIVE org_tree AS (
    -- Base case: root nodes
    SELECT id, name, parent_id, 0 AS depth
    FROM employees WHERE parent_id IS NULL
    UNION ALL
    -- Recursive case
    SELECT e.id, e.name, e.parent_id, ot.depth + 1
    FROM employees e
    JOIN org_tree ot ON e.parent_id = ot.id
)
SELECT * FROM org_tree ORDER BY depth, name;

HTAP Pattern — Separate Read/Write Paths

-- Write path: OLTP primary (PostgreSQL)
INSERT INTO orders (user_id, total, created_at) VALUES ($1, $2, NOW());

-- Read path: replica or analytics DB
-- Use logical replication to BigQuery/Snowflake for heavy aggregations
SELECT DATE_TRUNC('month', created_at), SUM(total)
FROM orders
GROUP BY 1 ORDER BY 1;
-- Route this to read replica, not primary

SCD Type 2 (Slowly Changing Dimensions)

-- Invalidate current record, insert new version
UPDATE dim_customers
SET valid_to = NOW(), is_current = FALSE
WHERE customer_id = $1 AND is_current = TRUE;

INSERT INTO dim_customers (customer_id, name, email, valid_from, valid_to, is_current)
VALUES ($1, $2, $3, NOW(), '9999-12-31', TRUE);

Anti-Patterns

Documentation Sources

Reference Files

(None yet — patterns are inline above. Add reference/cloud-platform-sql.md when cloud-specific patterns grow beyond this file.)

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.