Cheatsheets

SQL cheatsheet

Selects, joins, aggregates, and indexes you will actually type.

Query

Command / topicWhat it does
SELECT a, b FROM t WHERE x = 1;Filter rows
SELECT * FROM t ORDER BY created_at DESC LIMIT 20;Latest rows
SELECT COUNT(*) FROM t GROUP BY status;Aggregate by group
SELECT DISTINCT country FROM users;Unique values

Joins

Command / topicWhat it does
INNER JOIN b ON a.id = b.a_idMatching rows only
LEFT JOIN b ON a.id = b.a_idKeep left rows even without matches
EXISTS (SELECT 1 FROM b WHERE …)Correlated existence check

Schema helpers

Command / topicWhat it does
CREATE INDEX idx_users_email ON users(email);Speed up lookups
EXPLAIN QUERY PLAN …;Inspect how SQLite plans a query
BEGIN; … COMMIT;Wrap changes in a transaction

← All cheatsheets · Courses