Utility Empire

SQL - Quick Reference

Statements, filtering, joins, aggregations and modern constructs across the common SQL dialects. All rows are plain text - search to filter, click Copy to grab the pattern.

Reference rack
STATUSREADY
ROWS45
NETWORKLOCAL
MODEREFERENCE
Item / patternWhat it means / how to use it
SELECT col1, col2 FROM tRead specific columns from a table.
SELECT DISTINCT col FROM tUnique values only.
SELECT * FROM t WHERE col = vFilter rows by a condition.
ORDER BY col DESC / ORDER BY col ASCSort results, usually null-last or null-first per dialect.
LIMIT n / OFFSET mTake n rows / skip m rows - the old-school pagination pair.
UPDATE t SET col = v WHERE id = ?Modify matching rows; ALWAYS keep the WHERE clause.
DELETE FROM t WHERE id = ?Delete matching rows; ALWAYS keep the WHERE clause.
INSERT INTO t (a, b) VALUES (1, "x")Insert one row with explicit columns.
INSERT ... ON CONFLICT / ON DUPLICATE KEYThe upsert idioms of Postgres/SQLite and MySQL.
MERGE INTO / UPSERTDialect-specific merge patterns for full synchronization.
CREATE TABLE t (... )Define a table; add PRIMARY KEY, NOT NULL, UNIQUE, CHECK constraints.
ALTER TABLE t ADD COLUMN col TYPEEvolve a schema without dropping it.
DROP TABLE tRemove a table and its data - irreversible.
WHERE col IN (1,2,3)Membership test against a set.
WHERE col LIKE "a%""%" matches any run of characters, "_" matches exactly one.
WHERE col BETWEEN a AND bInclusive range check.
WHERE col IS NULL / IS NOT NULLNull checks - NULL never equals anything, not even NULL.
AND / OR / NOTBoolean combinators; use parentheses to control precedence.
COUNT(*) / SUM(col) / AVG(col) / MIN(col) / MAX(col)The core aggregate functions.
GROUP BY colBucket rows; every selected non-aggregate column must appear here.
HAVING COUNT(*) > 5Filter on aggregated results after grouping.
COUNT(DISTINCT col)Distinct-value count.
INNER JOIN b ON a.id = b.aidRows that match on both sides.
LEFT JOIN b ON a.id = b.aidAll left rows, NULLs wherever the right side is missing.
RIGHT JOIN / FULL OUTER JOINRight-side or both-sides completeness.
CROSS JOINCartesian product - watch for row explosion.
Self-joinJoin a table to itself with aliases for trees and hierarchies.
UNION / UNION ALLCombine result sets; ALL keeps duplicates.
WHERE col = (SELECT MAX(x) FROM t)A scalar subquery.
EXISTS (SELECT 1 ... )A cheap presence/absence test that stops at the first hit.
WITH cte AS (SELECT ... )A named subquery you can reference multiple times.
WITH RECURSIVE cte AS (... )Recursive CTEs for trees and graphs like org charts.
CASE WHEN cond THEN a ELSE b ENDConditional value per row.
COALESCE(a, b, 0)The first non-NULL argument.
CAST(x AS type) / x::typeExplicit type coercion.
DATE_TRUNC("day", ts) / EXTRACT(YEAR FROM ts)Truncate timestamps or pull parts out.
ROW_NUMBER() OVER (PARTITION BY g ORDER BY ...)Number rows within each partition.
SUM(x) OVER (PARTITION BY g)Window aggregate - running totals without collapsing rows.
DISTINCT ON (Postgres)First row per group, honoring your ORDER BY.
JSON extract / ->> and jsonb columnsQuery JSON documents stored in SQL columns.
CREATE INDEX idx ON t (col)Speed up lookups on that column; every insert pays a small write cost.
EXPLAIN / EXPLAIN ANALYZEThe query plan and real timings - step one of any slow-query hunt.
PRAGMA table_info(t) (SQLite)Introspect a SQLite table's schema.
STRFTIME / DATE_FORMATSQLite / MySQL date formatting functions.
BEGIN / COMMIT / ROLLBACKWrap multiple statements in one atomic transaction.