スキル一覧に戻る
aiskillstore

sql-patterns

by aiskillstore

sql-patternsは、other分野における実用的なスキルです。複雑な課題への対応力を強化し、業務効率と成果の質を改善します。

102🍴 3📅 2026年1月23日
GitHubで見るManusで実行

SKILL.md


name: sql-patterns description: "Quick reference for common SQL patterns, CTEs, window functions, and indexing strategies. Triggers on: sql patterns, cte example, window functions, sql join, index strategy, pagination sql." allowed-tools: "Read Write"

SQL Patterns

Quick reference for common SQL patterns.

CTE (Common Table Expressions)

WITH active_users AS (
    SELECT id, name, email
    FROM users
    WHERE status = 'active'
)
SELECT * FROM active_users WHERE created_at > '2024-01-01';

Chained CTEs

WITH
    active_users AS (
        SELECT id, name FROM users WHERE status = 'active'
    ),
    user_orders AS (
        SELECT user_id, COUNT(*) as order_count
        FROM orders GROUP BY user_id
    )
SELECT u.name, COALESCE(o.order_count, 0) as orders
FROM active_users u
LEFT JOIN user_orders o ON u.id = o.user_id;

Window Functions (Quick Reference)

FunctionUse
ROW_NUMBER()Unique sequential numbering
RANK()Rank with gaps (1, 2, 2, 4)
DENSE_RANK()Rank without gaps (1, 2, 2, 3)
LAG(col, n)Previous row value
LEAD(col, n)Next row value
SUM() OVERRunning total
AVG() OVERMoving average
SELECT
    date,
    revenue,
    LAG(revenue, 1) OVER (ORDER BY date) as prev_day,
    SUM(revenue) OVER (ORDER BY date) as running_total
FROM daily_sales;

JOIN Reference

TypeReturns
INNER JOINOnly matching rows
LEFT JOINAll left + matching right
RIGHT JOINAll right + matching left
FULL JOINAll rows, NULL where no match

Pagination

-- OFFSET/LIMIT (simple, slow for large offsets)
SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 40;

-- Keyset (fast, scalable)
SELECT * FROM products WHERE id > 42 ORDER BY id LIMIT 20;

Index Quick Reference

Index TypeBest For
B-treeRange queries, ORDER BY
HashExact equality only
GINArrays, JSONB, full-text
CoveringAvoid table lookup

Anti-Patterns

MistakeFix
SELECT *List columns explicitly
WHERE YEAR(date) = 2024WHERE date >= '2024-01-01'
NOT IN with NULLsUse NOT EXISTS
N+1 queriesUse JOIN or batch

Additional Resources

For detailed patterns, load:

  • ./references/window-functions.md - Complete window function patterns
  • ./references/indexing-strategies.md - Index types, covering indexes, optimization

スコア

総合スコア

60/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

+5
最近の活動

3ヶ月以内に更新

+5
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

レビュー

💬

レビュー機能は近日公開予定です