A compact SQL reference covering the clauses, joins, and functions you reach for daily. Standard ANSI SQL syntax that works across Postgres, MySQL, and SQLite.
SELECTSELECT col1, col2 FROM table; use * for all columns.WHEREFilter rows: WHERE price > 100 AND in_stock = true.ORDER BYSort: ORDER BY created_at DESC, name ASC.LIMIT / OFFSETLIMIT 10 OFFSET 20 for pagination.DISTINCTSELECT DISTINCT country removes duplicate rows.ASAlias columns or tables: SELECT total AS revenue.INWHERE status IN ('open', 'pending')BETWEENWHERE age BETWEEN 18 AND 65 (inclusive).LIKEWHERE name LIKE 'A%' matches names starting with A.IS NULLWHERE deleted_at IS NULL (use IS, not =).COALESCECOALESCE(nickname, name) returns the first non-null value.INNER JOINRows with matches in both tables.LEFT JOINAll left rows; nulls where the right has no match.RIGHT JOINAll right rows; nulls where the left has no match.FULL OUTER JOINAll rows from both sides, matched where possible.ONJoin condition: ON orders.user_id = users.idSelf joinJoin a table to itself with two aliases (e.g. e and m).COUNT / SUM / AVGAggregate functions: COUNT(*), SUM(amount), AVG(score).MIN / MAXSmallest and largest values in a column.GROUP BYAggregate per group: GROUP BY category.HAVINGFilter groups after aggregation: HAVING COUNT(*) > 5.Window functionsROW_NUMBER() OVER (PARTITION BY x ORDER BY y) for ranked rows.INSERTINSERT INTO t (a, b) VALUES (1, 2);UPDATEUPDATE t SET status = 'done' WHERE id = 5;DELETEDELETE FROM t WHERE id = 5; (always include WHERE).UPSERTINSERT ... ON CONFLICT (id) DO UPDATE SET ... (Postgres).TransactionsBEGIN; ...; COMMIT; or ROLLBACK; to undo.Want ready-to-use prompts to go with this cheatsheet?
Browse 30,000+ Prompts