Query, filter, aggregate, join and modify — the core SQL statements, ready to copy and adapt.
SELECT * FROM table;Return every column and row from a tableSELECT col1, col2 FROM table;Return only specific columnsSELECT * FROM table WHERE col = 'x';Filter rows by a conditionSELECT DISTINCT col FROM table;Return unique values of a columnSELECT * FROM table ORDER BY col DESC;Sort results by a columnSELECT * FROM table LIMIT 10;Return at most 10 rowsSELECT COUNT(*) FROM table;Count the rows in a tableSELECT col, COUNT(*) FROM table GROUP BY col;Count rows per groupSELECT AVG(col) FROM table;Average a numeric columnSELECT col, SUM(amt) FROM table GROUP BY col HAVING SUM(amt) > 100;Filter groups by an aggregateSELECT * FROM a JOIN b ON a.id = b.a_id;Inner join two tables on a keySELECT * FROM a LEFT JOIN b ON a.id = b.a_id;Keep all rows from the left tableSELECT * FROM a, b WHERE a.id = b.a_id;Older comma-style join syntaxINSERT INTO table (a, b) VALUES (1, 'x');Add a new rowUPDATE table SET col = 'x' WHERE id = 1;Change values in matching rowsDELETE FROM table WHERE id = 1;Remove matching rowsCREATE TABLE t (id INT, name TEXT);Create a new tableALTER TABLE t ADD COLUMN c INT;Add a column to a tableDROP TABLE t;Delete a table and its dataMost SQL work is built from a few patterns: SELECT … WHERE to read and filter, GROUP BY with aggregates like COUNT and SUM to summarise, JOIN to combine tables, and INSERT/UPDATE/DELETE to change data. Swap in your own table and column names as you copy.