SQL
SQL Cheatsheet
Standard SQL syntax for querying and modifying relational databases - works across MySQL, PostgreSQL, and SQL Server with minor dialect differences.
Querying
SELECT * FROM tableSelect all columns
SELECT a, b FROM tableSelect specific columns
WHERE conditionFilter rows
ORDER BY col DESCSort results
LIMIT 10Limit number of rows returned
DISTINCTRemove duplicate rows
Joins
INNER JOIN b ON a.id = b.a_idOnly matching rows in both tables
LEFT JOIN b ON a.id = b.a_idAll rows from left, matched rows from right
RIGHT JOIN b ON a.id = b.a_idAll rows from right, matched rows from left
Aggregation
COUNT(*)Count rows
SUM(col)Sum a column
AVG(col)Average of a column
GROUP BY colGroup rows for aggregation
HAVING conditionFilter after grouping
Modifying data
INSERT INTO t (a,b) VALUES (1,2)Insert a row
UPDATE t SET a=1 WHERE id=1Update rows
DELETE FROM t WHERE id=1Delete rows
CREATE TABLE t (id INT PRIMARY KEY)Create a table