SQL Quick Reference
A concise reference card for common SQL syntax. Click any topic in the learning path for detailed explanations.
Querying Data
| Syntax | Description |
|---|---|
SELECT col1, col2 FROM table | Retrieve specific columns |
SELECT * FROM table | Retrieve all columns |
SELECT DISTINCT col FROM table | Unique values only |
SELECT col AS alias FROM table | Rename column in output |
Filtering
| Syntax | Description |
|---|---|
WHERE condition | Filter rows |
WHERE col IN (v1, v2) | Match against a list |
WHERE col BETWEEN a AND b | Range filter (inclusive) |
WHERE col LIKE '%pattern%' | Pattern matching |
WHERE col IS NULL | Check for missing values |
WHERE col IS NOT NULL | Check for existing values |
WHERE c1 AND c2 | Both conditions true |
WHERE c1 OR c2 | At least one condition true |
Sorting & Limiting
| Syntax | Description |
|---|---|
ORDER BY col ASC | Sort ascending (default) |
ORDER BY col DESC | Sort descending |
ORDER BY col NULLS FIRST | Place NULLs at the top |
ORDER BY col NULLS LAST | Place NULLs at the bottom |
LIMIT n | Return at most n rows |
LIMIT n OFFSET m | Skip m rows, return n |
Aggregation
| Syntax | Description |
|---|---|
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 col | Group rows for aggregation |
HAVING condition | Filter groups |
Joins
| Syntax | Description |
|---|---|
INNER JOIN b ON a.key = b.key | Only matching rows |
LEFT JOIN b ON a.key = b.key | All left rows + matches |
RIGHT JOIN b ON a.key = b.key | All right rows + matches (SQLite ≥ 3.39) |
FULL OUTER JOIN b ON a.key = b.key | All rows from both sides (SQLite ≥ 3.39) |
CROSS JOIN b | All combinations |
Self join: FROM t a JOIN t b | Table joined to itself |
Subqueries & CTEs
| Syntax | Description |
|---|---|
WHERE col IN (SELECT ...) | Subquery in filter |
WHERE EXISTS (SELECT ...) | Check for existence |
WITH cte AS (SELECT ...) SELECT ... | Named temp result set |
Window Functions
| Syntax | Description |
|---|---|
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)
| Syntax | Description |
|---|---|
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 / ROLLBACK | Transaction control |
Defining Schema (DDL)
| Syntax | Description |
|---|---|
CREATE TABLE t (col TYPE CONSTRAINT, ...) | Create a new table |
ALTER TABLE t ADD COLUMN c TYPE | Add a column |
ALTER TABLE t RENAME TO new_name | Rename a table |
DROP TABLE t | Delete a table and its data |
Other
| Syntax | Description |
|---|---|
CASE WHEN cond THEN val ELSE val END | Conditional 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 ALL | Combine result sets |
INTERSECT | Common rows (MySQL ≥ 8.0.31) |
EXCEPT | Rows in first but not second (MySQL ≥ 8.0.31; Oracle calls this MINUS) |