agentleFS
kumaran-iskumaran-is/claude-code-onboarding/.claude/skills/sql-pro/SKILL.md

sql-pro

Skill36 starsChanged 6 months agoMITView on GitHub

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 workloads.

13 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 11 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

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

    line 18
  2. Very specific:

    Run EXPLAIN ANALYZE (PostgreSQL) or query profile (BigQuery/Snowflake)

    line 62
  3. Distinctive:

    Use read replicas for heavy analytics queries on OLTP primary

    line 67

“Common” is measured: kept by at least 6 in 10 of the 11 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Iron Law

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

When to Use This Skill

  • Writing complex SQL queries or analytics (window functions, recursive CTEs, OLAP)
  • Designing SQL for cloud-native platforms (BigQuery, Snowflake, Redshift, Aurora)
  • Building HTAP or hybrid analytical/transactional systems
  • Migrating from OLTP-only PostgreSQL to an analytics tier
  • Dimensional modeling, data vault, star/snowflake schemas
  • Integrating machine learning with SQL workloads
  • Time-series analysis (TimescaleDB, InfluxDB, Apache Druid)

Do Not Use This Skill When

  • You only need ORM-level guidance (use python-dev, nestjs-api, or java-spring-api)
  • The system is non-SQL or document-only (use database-schema-designer for Firestore patterns)
  • You need query optimization patterns for PostgreSQL OLTP (use sql-optimization-patterns)
  • You cannot access query plans or schema details

Quick Reference

WorkloadPlatformKey Consideration
OLTP (transactional)PostgreSQLNormalize to 3NF, index FKs and WHERE cols
OLAP (analytics)BigQuery / Snowflake / RedshiftDenormalize, columnar storage, partition by date
HTAP (both)CockroachDB / TiDBRead replicas for analytics, avoid read-your-own-writes latency
Time-seriesTimescaleDB / InfluxDBHypertables, continuous aggregates, retention policies
Data warehouseRedshift / DatabricksStar schema, materialized views, query concurrency
Graph + SQLNeo4j / Amazon NeptuneCypher for traversal, SQL for aggregation

Process

Phase 1 — Understand the Workload

  • Classify: OLTP / OLAP / HTAP / time-series / data warehouse
  • Identify read/write ratio, peak concurrency, data volume (rows, GB)
  • Confirm platform and version (PostgreSQL 16, BigQuery, Snowflake Enterprise, etc.)

Phase 2 — Design the Query or Schema

  • Apply appropriate normalization or denormalization for workload type
  • For OLAP: star schema (fact + dimensions), SCD Type 2 for slowly changing dims
  • For data vault: hubs, links, satellites pattern
  • For event sourcing: append-only events table, aggregate projections

Phase 3 — Optimize and Validate

  • Run EXPLAIN ANALYZE (PostgreSQL) or query profile (BigQuery/Snowflake)
  • Identify sequential scans, hash joins on large tables, spill to disk
  • Apply indexes, partitioning, or materialization as evidence dictates

Phase 4 — Production Readiness

  • Use read replicas for heavy analytics queries on OLTP primary
  • Apply LIMIT + cursor pagination for large result sets
  • Set statement_timeout for user-facing queries
  • Enable connection pooling (PgBouncer) for high-concurrency workloads

Platform-Specific Capabilities

PostgreSQL (Primary Stack)

  • Window functions: OVER (PARTITION BY ... ORDER BY ...)
  • Recursive CTEs: hierarchical data traversal
  • JSON/JSONB: @>, #>>, jsonb_path_query
  • Full-text: to_tsvector, ts_rank, GIN indexes
  • Temporal: timestamptz, AT TIME ZONE, generate_series for gaps
  • Extensions: pg_stat_statements, pg_trgm, uuid-ossp, pgcrypto

Cloud Analytics Platforms

  • BigQuery: Standard SQL, partitioned tables, clustering, INFORMATION_SCHEMA
  • Snowflake: Zero-copy cloning, time-travel, micro-partitioning, tasks + streams
  • Redshift: Distribution keys, sort keys, VACUUM + ANALYZE cadence
  • Databricks: Delta Lake, MERGE INTO, streaming + batch unification

Time-Series

  • TimescaleDB: Hypertables, continuous aggregates, compression policies
  • InfluxDB: Flux queries, bucket retention, downsampling tasks
  • Apache Druid: Real-time ingestion, rollup, approximate aggregations (HLL, quantiles)

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

  • Running heavy OLAP aggregations on OLTP primary — use read replica or separate analytics DB
  • SELECT * on wide fact tables in analytics workloads — columns are stored separately in columnar DBs
  • Non-partitioned tables for time-series data exceeding 1M rows — always partition by time
  • Correlated subqueries in analytical queries — always transform to JOINs or window functions
  • Implicit type casting in JOIN conditions — prevents index usage, causes full scans
  • DDL inside transactions on BigQuery/Snowflake — not supported; manage schema changes separately

Documentation Sources

  • PostgreSQL docs: Query MCP context7 with library ID /postgresql/postgresql
  • BigQuery: Query MCP context7 with library ID /googleapis/google-cloud-bigquery
  • Snowflake SQL reference: WebFetch from Snowflake documentation
  • TimescaleDB: Query MCP context7

Reference Files

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

Related Skills

  • sql-optimization-patterns — PostgreSQL OLTP query tuning, EXPLAIN analysis, N+1 fixes, cursor pagination
  • database-schema-designer — Schema design, Flyway migrations, normalization, FK/index rules
  • vector-database — pgvector, HNSW/IVFFlat, RAG pipelines
  • python-dev — SQLAlchemy async, asyncpg, FastAPI integration
  • java-spring-api — R2DBC reactive PostgreSQL
  • nestjs-api — Prisma ORM, raw SQL via Prisma

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.