sql-programming

Applies SQL formatting, debugging, indexing, and query-shaping conventions to non-spatial database code.

2.2k|433|Updated May 22, 2012
One-click install
npx skills add https://github.com/postgis/postgis --skill sql-programming
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-programming
Source: https://github.com/postgis/postgis/tree/main/doc/skills/sql-programming
Command: npx skills add https://github.com/postgis/postgis --skill sql-programming

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing maintainable SQL is hard: inconsistent formatting, missing comments, non-idempotent scripts, and subtle query anti-patterns lead to slow queries and confusing codebases. This Skill provides a concrete set of conventions for general SQL work that is not specific to PostGIS spatial data.

Core Features & Use Cases

  • Style and Documentation Rules: Enforces lowercase SQL, descriptive comments on every CREATE statement or CTE, consistent formatting, and absolute values (e.g., store "birthday" instead of "age").
  • Indexing Guidance: Recommends BRIN indexes for large naturally ordered tables and covering indexes with INCLUDE for cache-table lookups.
  • Debugging and Migration Practices: Promotes idempotent SQL files (DROP IF EXISTS + CREATE), paired up/down migrations, meaningful error messages, and avoiding fragile fallbacks like defaulting coordinates to zero.
  • SQL Gotcha Rewrites: Rewrites common anti-patterns such as sum(case when ...) into count() filter (where ...), row_number() = 1 into order by + limit 1, and tags ->> 'key' = 'value' into index-friendly tags @> '{"key": "value"}'.
  • Use Case: When reviewing a migration script or refactoring a slow reporting query, apply these rules to produce clean, idempotent, index-aware SQL.

Quick Start

Review my SQL migration file and rewrite it following the sql-programming conventions for formatting, idempotency, and query optimization.

Frequently Asked Questions about sql-programming

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write idempotent SQL migration scripts?

Make each script safe to re-run by using DROP TABLE IF EXISTS before CREATE TABLE, and add comments explaining each step. When the project expects reversible migrations, create both an up migration and a matching down/rollback migration.

How to rewrite sum case when queries in SQL?

Replace `sum(case when A then 1 else 0 end)` with the clearer `count() filter (where A)`. Similarly, `row_number() ... = 1` patterns can often be rewritten as `order by` plus `limit 1`, optionally with `distinct on` or `lateral`.

When should I use a BRIN index in PostgreSQL?

Use BRIN indexes for very large, naturally ordered tables that receive ad-hoc range queries, since they are far smaller than B-tree indexes. For cache tables with a primary key, consider adding frequently read columns via INCLUDE for faster index-only lookups.

Does jsonb containment perform better than the ->> operator?

Yes. Writing `tags @> '{"key": "value"}'` instead of `tags ->> 'key' = 'value'` can use a GIN index on the jsonb column, making lookups significantly faster on large tables.

Why is my table not ordered after inserting rows in order?

SQL tables have no guaranteed scan order even if rows were inserted in a specific sequence. You must always add an explicit ORDER BY clause to any query that depends on row ordering.