SQL Cheat Sheet
SELECT
| Query / Syntax | Description |
|---|---|
SELECT * FROM table |
Retrieve all columns from a table |
SELECT col1, col2 FROM table |
Retrieve specific columns |
SELECT DISTINCT col FROM table |
Return only unique values |
SELECT col AS alias FROM table |
Rename a column in the result |
SELECT * FROM table LIMIT 10 |
Return only the first 10 rows |
SELECT * FROM table LIMIT 10 OFFSET 20 |
Skip 20 rows, return next 10 |
Filtering
| Query / Syntax | Description |
|---|---|
WHERE col = value |
Exact match filter |
WHERE col != value |
Not equal filter |
WHERE col > value AND col2 < value2 |
Multiple conditions with AND |
WHERE col = a OR col = b |
Either condition |
WHERE col IN (a, b, c) |
Match any value in a list |
WHERE col NOT IN (a, b) |
Exclude values in a list |
WHERE col BETWEEN 10 AND 20 |
Range filter (inclusive) |
WHERE col LIKE 'abc%' |
Starts with abc |
WHERE col LIKE '%abc%' |
Contains abc anywhere |
WHERE col IS NULL |
Match rows where column has no value |
WHERE col IS NOT NULL |
Match rows where column has a value |
Sorting
| Query / Syntax | Description |
|---|---|
ORDER BY col ASC |
Sort ascending (default) |
ORDER BY col DESC |
Sort descending |
ORDER BY col1 ASC, col2 DESC |
Sort by multiple columns |
Aggregates
| Query / Syntax | Description |
|---|---|
COUNT(*) |
Count all rows |
COUNT(col) |
Count non-null values in a column |
SUM(col) |
Sum of a numeric column |
AVG(col) |
Average of a numeric column |
MIN(col) / MAX(col) |
Smallest / largest value |
GROUP BY col |
Group rows with the same value |
HAVING COUNT(*) > 5 |
Filter groups (like WHERE but for aggregates) |
Joins
| Query / Syntax | Description |
|---|---|
INNER JOIN b ON a.id = b.a_id |
Rows that match in both tables |
LEFT JOIN b ON a.id = b.a_id |
All rows from left, matched rows from right (NULLs if no match) |
RIGHT JOIN b ON a.id = b.a_id |
All rows from right, matched rows from left |
FULL OUTER JOIN b ON a.id = b.a_id |
All rows from both tables |
CROSS JOIN b |
Every combination of rows from both tables |
JOIN b ON a.id = b.a_id JOIN c ON b.id = c.b_id |
Chain multiple joins |
Insert / Update / Delete
| Query / Syntax | Description |
|---|---|
INSERT INTO table (col1, col2) VALUES (v1, v2) |
Insert a new row |
INSERT INTO table (col1, col2) VALUES (v1, v2), (v3, v4) |
Insert multiple rows |
UPDATE table SET col = value WHERE id = 1 |
Update specific rows |
DELETE FROM table WHERE id = 1 |
Delete specific rows |
DELETE FROM table |
Delete all rows (keep table structure) |
TRUNCATE TABLE table |
Quickly delete all rows |
Tables & Schema
| Query / Syntax | Description |
|---|---|
CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT NOT NULL) |
Create a table |
ALTER TABLE t ADD COLUMN col TEXT |
Add a column |
ALTER TABLE t DROP COLUMN col |
Remove a column |
ALTER TABLE t RENAME TO new_name |
Rename a table |
DROP TABLE t |
Delete a table permanently |
CREATE INDEX idx ON t (col) |
Create an index to speed up queries |
DROP INDEX idx |
Remove an index |
Subqueries & CTEs
| Query / Syntax | Description |
|---|---|
SELECT * FROM t WHERE id IN (SELECT id FROM t2) |
Subquery in WHERE |
SELECT * FROM (SELECT col FROM t) AS sub |
Subquery as a derived table |
WITH cte AS (SELECT ...) SELECT * FROM cte |
Common Table Expression (CTE) |
SELECT *, ROW_NUMBER() OVER (ORDER BY col) FROM t |
Window function — row number |
SELECT *, SUM(col) OVER (PARTITION BY grp) FROM t |
Window function — running sum per group |