SQLチートシート
SQLの構文、結合、関数、データ型のクイックリファレンスです。
動作の仕組み
SQL evaluates logically as FROM/JOIN -> WHERE -> GROUP BY -> HAVING -> SELECT/WINDOW -> DISTINCT -> ORDER BY -> LIMIT, even though `SELECT` is written first.
サバイバルキット
最初に覚えるべき最小限のコマンド。
42 件のコマンド
Select, Filter & Pagination
Filter predicates safely, avoid NULL traps, sort, and paginate efficiently.
- 基本
Filter rows, sort deterministically, and cap returned rows
SELECT <cols> FROM <table> WHERE <cond> ORDER BY <col> DESC LIMIT <n>;コマンドを組み立てる
SELECT <cols> FROM <table> WHERE <cond> ORDER BY <col> DESC LIMIT <n>;値は入力した通りにそのまま挿入されます。空白や特殊文字を含む場合は、必要に応じて引用符で囲んでください。
使いどころ
Adding a tie-breaker column to `ORDER BY` guarantees repeatable pagination order.
実例
SELECT id, email, created_at FROM users WHERE status = 'active' ORDER BY created_at DESC, id DESC LIMIT 25;
Joins & Set Operations
Combine related tables cleanly and find orphan or unmatched rows.
Aggregation, CTEs & Window Functions
Group metrics, stage readable queries with CTEs, and rank without collapsing rows.
Insert, Upsert, Update & Delete
Write idempotent mutations safely inside transactions without locking accidents.
Schema, Constraints, JSON & Performance
Design tables, composite indexes, JSON queries, and inspect EXPLAIN plans.