sql-patterns_skill

This skill helps you write and optimize SQL queries using common patterns, CTEs, and window functions for faster data retrieval.
  • Shell

8

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill 0xdarkmatter/claude-mods --skill sql-patterns

  • SKILL.md2.5 KB

Overview

This skill is a compact reference for common SQL patterns, including CTEs, window functions, joins, pagination, and indexing strategies. It delivers clear examples and quick rules-of-thumb to speed up query writing and optimization. Use it to validate patterns, avoid anti-patterns, and choose appropriate indexes. The content is focused on practical, copy-pasteable snippets.

How this skill works

The skill provides short, annotated examples for common tasks: building CTEs, chaining CTEs, using window functions, and applying joins and pagination. It lists index types and their ideal use cases, plus common anti-patterns with concise fixes. You can quickly look up the right pattern, adapt the SQL sample, and apply best practices to improve performance and readability.

When to use it

  • Composing complex queries that benefit from CTEs or chained CTEs
  • Implementing analytics or running row-based calculations with window functions
  • Choosing a pagination strategy for APIs (OFFSET/LIMIT vs keyset)
  • Selecting index types for query patterns (B-tree, GIN, hash, covering)
  • Checking for common anti-patterns before deploying queries

Best practices

  • List explicit columns instead of SELECT * to reduce I/O and prevent unexpected schema changes
  • Prefer range filters (date >= ...) over functions on columns to allow index use
  • Use NOT EXISTS instead of NOT IN when NULLs may be present
  • Favor keyset pagination for large offsets; use OFFSET/LIMIT only for small, simple pages
  • Create covering indexes for frequent SELECT columns to avoid table lookups

Example use cases

  • Generate an active user list, then join aggregated order counts via chained CTEs
  • Compute running totals and previous-period comparisons using LAG/ROW_NUMBER/SUM OVER
  • Paginate product lists in an API using keyset pagination for high-offset performance
  • Choose a GIN index for JSONB array searches and a B-tree for ORDER BY and range filters
  • Replace N+1 queries by joining or batching related rows in a single query

FAQ

Use CTEs for clarity, reuse, and complex multi-step logic. For single-use, simple subqueries can be marginally faster in some engines, but clarity often favors CTEs.

How do I pick between OFFSET/LIMIT and keyset pagination?

Use OFFSET/LIMIT for small offsets and simple UIs. Use keyset (seek) pagination for large datasets or high-offset pages to avoid increasing cost as offset grows.

Which index type should I choose for JSONB searches?

Use a GIN index for JSONB containment and array searches. B-tree is better for range queries and ORDER BY on scalar columns.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
sql-patterns skill by 0xdarkmatter/claude-mods | VeilStrat