Back to Cheatsheets
Database

SQL Basics

Essential SQL queries

SELECT Queries

4 items
CommandDescription
SELECT * FROM table
Select all columns
SELECT col1, col2 FROM table
Select specific columns
SELECT DISTINCT col FROM table
Unique values only
SELECT COUNT(*) FROM table
Count rows

Filtering

7 items
CommandDescription
WHERE col = value
Exact match
WHERE col > value
Comparison
WHERE col LIKE "%text%"
Pattern match
WHERE col IN (1, 2, 3)
Match any in list
WHERE col IS NULL
Check for null
WHERE a AND b
Multiple conditions
WHERE a OR b
Either condition

Sorting & Limiting

4 items
CommandDescription
ORDER BY col ASC
Sort ascending
ORDER BY col DESC
Sort descending
LIMIT 10
Limit results
OFFSET 20
Skip rows

Joins

3 items
CommandDescription
INNER JOIN t2 ON t1.id = t2.id
Match in both tables
LEFT JOIN t2 ON t1.id = t2.id
All from left + matches
RIGHT JOIN t2 ON t1.id = t2.id
All from right + matches

Modifying Data

3 items
CommandDescription
INSERT INTO table (cols) VALUES (vals)
Insert row
UPDATE table SET col = val WHERE ...
Update rows
DELETE FROM table WHERE ...
Delete rows

Grouping

3 items
CommandDescription
GROUP BY col
Group rows
HAVING COUNT(*) > 5
Filter groups
SUM(col), AVG(col), MAX(col)
Aggregate functions