SQL Quick Reference

A concise reference card for common SQL syntax. Click any topic in the learning path for detailed explanations.

Querying Data

SyntaxDescription
SELECT col1, col2 FROM tableRetrieve specific columns
SELECT * FROM tableRetrieve all columns
SELECT DISTINCT col FROM tableUnique values only
SELECT col AS alias FROM tableRename column in output

Filtering

SyntaxDescription
WHERE conditionFilter rows
WHERE col IN (v1, v2)Match against a list
WHERE col BETWEEN a AND bRange filter (inclusive)
WHERE col LIKE '%pattern%'Pattern matching
WHERE col IS NULLCheck for missing values
WHERE col IS NOT NULLCheck for existing values
WHERE c1 AND c2Both conditions true
WHERE c1 OR c2At least one condition true

Sorting & Limiting

SyntaxDescription
ORDER BY col ASCSort ascending (default)
ORDER BY col DESCSort descending
ORDER BY col NULLS FIRSTPlace NULLs at the top
ORDER BY col NULLS LASTPlace NULLs at the bottom
LIMIT nReturn at most n rows
LIMIT n OFFSET mSkip m rows, return n

Aggregation

SyntaxDescription
COUNT(*)Count all rows
COUNT(col)Count non-NULL values
COUNT(DISTINCT col)Count unique non-NULL values
SUM(col) / AVG(col)Sum / Average
MIN(col) / MAX(col)Minimum / Maximum
GROUP BY colGroup rows for aggregation
HAVING conditionFilter groups

Joins

SyntaxDescription
INNER JOIN b ON a.key = b.keyOnly matching rows
LEFT JOIN b ON a.key = b.keyAll left rows + matches
RIGHT JOIN b ON a.key = b.keyAll right rows + matches (SQLite ≥ 3.39)
FULL OUTER JOIN b ON a.key = b.keyAll rows from both sides (SQLite ≥ 3.39)
CROSS JOIN bAll combinations
Self join: FROM t a JOIN t bTable joined to itself

Subqueries & CTEs

SyntaxDescription
WHERE col IN (SELECT ...)Subquery in filter
WHERE EXISTS (SELECT ...)Check for existence
WITH cte AS (SELECT ...) SELECT ...Named temp result set

Window Functions

SyntaxDescription
ROW_NUMBER() OVER (ORDER BY col)Unique row numbers
RANK() OVER (ORDER BY col)Rank with gaps
DENSE_RANK() OVER (...)Rank without gaps
LAG(col, 1) OVER (...)Previous row value
LEAD(col, 1) OVER (...)Next row value
SUM(col) OVER (PARTITION BY g ORDER BY d)Running aggregate per group

Modifying Data (DML)

SyntaxDescription
INSERT INTO t (cols) VALUES (...)Add new rows
INSERT INTO t SELECT ...Insert from a query
UPDATE t SET col = v WHERE ...Change existing rows
DELETE FROM t WHERE ...Remove rows
BEGIN / COMMIT / ROLLBACKTransaction control

Defining Schema (DDL)

SyntaxDescription
CREATE TABLE t (col TYPE CONSTRAINT, ...)Create a new table
ALTER TABLE t ADD COLUMN c TYPEAdd a column
ALTER TABLE t RENAME TO new_nameRename a table
DROP TABLE tDelete a table and its data

Other

SyntaxDescription
CASE WHEN cond THEN val ELSE val ENDConditional logic
COALESCE(col, default)Replace NULL — first non-NULL of args
IFNULL(col, default)Two-argument NULL replacement (SQLite/MySQL)
NULLIF(a, b)Returns NULL if a = b, else a
UNION / UNION ALLCombine result sets
INTERSECTCommon rows (MySQL ≥ 8.0.31)
EXCEPTRows in first but not second (MySQL ≥ 8.0.31; Oracle calls this MINUS)