SELECT col1, col2 FROM tableSelect specific columns
SELECT * FROM tableSelect all columns
SELECT DISTINCT col FROM tableSelect unique values only
WHERE conditionFilter rows by condition
WHERE col IN (v1, v2)Match any value in list
WHERE col BETWEEN a AND bMatch range (inclusive)
WHERE col LIKE "pattern"Pattern match (% = any, _ = single char)
WHERE col IS NULLCheck for NULL values
WHERE col IS NOT NULLCheck for non-NULL values
AND / OR / NOTCombine conditions
ORDER BY col ASC|DESCSort results
LIMIT n OFFSET mLimit results with offset
SELECT col AS aliasColumn alias
EXISTS (subquery)Check if subquery returns rows
CASE WHEN cond THEN val ELSE val ENDConditional expression
INNER JOIN t2 ON t1.id = t2.idRows matching in both tables
LEFT JOIN t2 ON t1.id = t2.idAll from left + matching from right
RIGHT JOIN t2 ON t1.id = t2.idAll from right + matching from left
FULL OUTER JOIN t2 ON t1.id = t2.idAll rows from both tables
CROSS JOIN t2Cartesian product of both tables
SELF JOIN: SELECT ... FROM t1 a, t1 bJoin table with itself
NATURAL JOIN t2Join on columns with same name
COUNT(*)Count all rows
COUNT(DISTINCT col)Count unique values
SUM(col)Sum of values
AVG(col)Average of values
MIN(col) / MAX(col)Minimum / Maximum value
GROUP BY colGroup rows by column
HAVING conditionFilter groups (after GROUP BY)
GROUP_CONCAT(col)Concatenate grouped values (MySQL)
STRING_AGG(col, sep)Concatenate grouped values (PostgreSQL)
INSERT INTO table (cols) VALUES (vals)Insert a new row
INSERT INTO t1 SELECT ... FROM t2Insert from another table
UPDATE table SET col = val WHERE ...Update existing rows
DELETE FROM table WHERE ...Delete rows
TRUNCATE TABLE tableDelete all rows (faster than DELETE)
REPLACE INTO table (cols) VALUES (vals)Insert or replace (MySQL)
UPSERT / ON CONFLICT DO UPDATEInsert or update on conflict (PostgreSQL)
CREATE TABLE t (col TYPE, ...)Create a new table
ALTER TABLE t ADD col TYPEAdd a column
ALTER TABLE t DROP COLUMN colRemove a column
ALTER TABLE t RENAME TO new_nameRename a table
DROP TABLE tDelete a table
CREATE INDEX idx ON t (col)Create an index
DROP INDEX idxDelete an index
CREATE VIEW v AS SELECT ...Create a view
PRIMARY KEYUniquely identifies each row
FOREIGN KEY REFERENCES t(col)Reference another table
UNIQUEEnsure unique values in column
NOT NULLColumn cannot be NULL
DEFAULT valueDefault column value
CHECK (condition)Validate column values
ROW_NUMBER() OVER (ORDER BY col)Sequential row number
RANK() OVER (ORDER BY col)Rank with gaps for ties
DENSE_RANK() OVER (ORDER BY col)Rank without gaps
LAG(col, n) OVER (ORDER BY col)Value from n rows before
LEAD(col, n) OVER (ORDER BY col)Value from n rows after
PARTITION BY colDivide rows into groups for window functions
SUM(col) OVER (PARTITION BY g)Running total within partition
NTILE(n) OVER (ORDER BY col)Divide rows into n buckets
CONCAT(s1, s2)Concatenate strings
LENGTH(s) / LEN(s)String length
UPPER(s) / LOWER(s)Change case
TRIM(s)Remove leading/trailing whitespace
SUBSTRING(s, start, len)Extract substring
REPLACE(s, old, new)Replace occurrences
COALESCE(v1, v2, ...)Return first non-NULL value
CAST(val AS TYPE)Convert to another data type
browserutils
SQL Cheatsheet