SQL Idioms and Patterns

Rewrite SQL queries using CTEs, explicit JOINs, and index-aware patterns.

150|48|Updated Jan 24, 2026
One-click install
npx skills add https://github.com/irahardianto/awesome-agv --skill sql-idioms-and-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: SQL Idioms and Patterns
Source: https://github.com/irahardianto/awesome-agv/tree/main/.agents/skills/sql-idioms
Command: npx skills add https://github.com/irahardianto/awesome-agv --skill sql-idioms-and-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you avoid fragile, unreadable, or inefficient SQL by enforcing idiomatic patterns that remain correct under growth, concurrency, and schema evolution.

Core Features & Use Cases

  • Query readability and maintainability: Prefer CTEs over nested subqueries and use explicit JOINs for clear intent.
  • Performance and scalability: Use EXPLAIN (ANALYZE, BUFFERS), avoid SELECT *, design indexes (including composite/partial/covering), and choose keyset pagination over OFFSET at scale.
  • Migration-safe and correct data operations: Apply idempotent DDL strategies, safe constraint/index creation patterns, UPSERT for race-free updates, and transaction-safe locking patterns.

Example: You are building a task queue and need a query that reliably selects the next pending job under concurrency; this Skill guides you to use SKIP LOCKED, set appropriate timeouts, and structure indexes so throughput stays high as the table grows.

Quick Start

Use the SQL Idioms and Patterns skill to rewrite the provided SQL query to use CTEs, explicit joins, safe parameterization, and index-aware performance improvements while ensuring migration-safe schema changes.

Frequently Asked Questions about SQL Idioms and Patterns

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

FAQPage Schema
How do I optimize PostgreSQL queries for high concurrency?

Optimize PostgreSQL queries for concurrency by applying EXPLAIN-based tuning, using parameterized queries, and implementing SKIP LOCKED for reliable task processing. Designing appropriate composite and partial indexes ensures high throughput as concurrent write volumes scale.

What is the best way to handle database migrations without downtime?

Handle migrations without downtime by adopting idempotent DDL strategies and safe index creation patterns. Apply transaction-safe locking with consistent lock ordering to ensure schema changes remain correct and non-blocking during concurrent operations.

How do I write a race-free UPSERT query in SQL?

Write a race-free UPSERT query by using built-in SQL UPSERT operations designed for concurrent writes. This pattern safely handles insert-or-update scenarios without locking conflicts, ensuring data integrity during parallel transaction execution.

When should I use keyset pagination instead of OFFSET in SQL?

Use keyset pagination instead of OFFSET when scaling large datasets to avoid performance degradation. Keyset pagination maintains consistent query speed by leveraging indexed columns, whereas OFFSET slows down significantly as it scans deeper into table rows.

How do I select the next pending job in a task queue using SQL?

Select the next pending job using the SKIP LOCKED clause to bypass rows already locked by concurrent workers. Combine this with appropriate timeouts and structured indexes to maintain high throughput and reliable job processing under concurrent loads.

Does query refactoring with CTEs improve SQL readability?

Query refactoring with CTEs improves SQL readability by replacing nested subqueries with clear, sequential logic blocks. Pairing CTEs with explicit JOINs clarifies query intent and maintains performance when combined with index-aware optimization strategies.