sql-patterns

Provide compact SQL pattern references for CTEs, window functions, joins, and indexing.

29|6|Updated Nov 27, 2025
One-click install
npx skills add https://github.com/0xDarkMatter/claude-mods --skill sql-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-patterns
Source: https://github.com/0xDarkMatter/claude-mods/tree/main/skills/sql-patterns
Command: npx skills add https://github.com/0xDarkMatter/claude-mods --skill sql-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill eliminates the time-consuming process of searching for SQL syntax examples and optimization patterns, giving you expert-level database knowledge on demand.

Core Features & Use Cases

  • CTE & Window Functions: Create complex queries with Common Table Expressions and analytical functions.
  • JOIN & Index Strategies: Optimize database performance with proper indexing and join patterns.
  • Use Case: When you need to write a recursive CTE for organizational hierarchies or calculate running totals with window functions.

Quick Start

Show me a recursive CTE example for employee reporting structures.

Frequently Asked Questions about sql-patterns

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

FAQPage Schema
How do I write a recursive CTE to handle hierarchical data like organizational structures?

Recursive CTEs use WITH clauses to anchor a base query, then iteratively join to themselves. Start with your root rows (e.g., top-level employees), then UNION ALL to join each level to its children, specifying a termination condition. This pattern works across PostgreSQL and other SQL systems for org charts, bill-of-materials, and reporting hierarchies.

What's the best way to optimize slow SQL queries using indexes and join strategies?

Index on columns in WHERE clauses and JOIN conditions first. Analyze join order—filter early and join smallest result sets last. Use EXPLAIN to see execution plans. Index strategies vary by join type: INNER JOINs benefit most from indexes on foreign keys, while LEFT JOINs require indexes on the joined table's key columns to avoid full table scans.

How do I calculate running totals and moving averages using window functions?

Window functions like SUM() OVER (ORDER BY column) compute aggregates across row ranges without collapsing groups. Use ROWS BETWEEN to define the window frame—UNBOUNDED PRECEDING for running totals, or N PRECEDING for moving averages. Partition by relevant columns to reset calculations per group.

When should I use CTEs versus subqueries or temporary tables?

CTEs improve readability for complex queries and enable recursion, which subqueries cannot. Use CTEs when multiple subqueries repeat logic or when recursion is needed. Subqueries are lighter-weight for single-use cases; temporary tables persist across statements but require cleanup, making CTEs the middle ground for multi-step logic.

Can I use window functions and CTEs together in a single query?

Yes. Define a CTE to prepare intermediate results, then apply window functions in the main SELECT. This pattern is common for hierarchical aggregations—build the hierarchy with a recursive CTE, then rank or sum rows using window functions to produce final analytics.

What are common anti-patterns to avoid when writing complex SQL queries?

Avoid Cartesian products from unspecified join conditions. Don't use SELECT * with joins unless you need all columns—specify explicitly to reduce memory. Avoid correlated subqueries in loops; use joins instead. Don't over-index; each index slows writes. Test EXPLAIN output to catch inefficient query plans before deploying.