Last updated: April 12, 2026
SQL Commands Cheatsheet
Quick Answer
This cheatsheet covers 30 essential SQL commands: SELECT queries, JOINs, aggregation (GROUP BY, HAVING), data modification (INSERT, UPDATE, DELETE), and schema management (CREATE, ALTER, DROP).
Queries
| Command | Description |
|---|---|
| SELECT col FROM table | Retrieve specific columns from a table SELECT name, email FROM users |
| SELECT * FROM table | Retrieve all columns (avoid in production) |
| SELECT DISTINCT col FROM table | Return only unique values SELECT DISTINCT country FROM users |
| WHERE condition | Filter rows by condition SELECT * FROM users WHERE age > 18 |
| AND / OR / NOT | Combine conditions WHERE age > 18 AND country = 'US' |
| IN (values) | Match any value in a list WHERE status IN ('active', 'pending') |
| BETWEEN a AND b | Match values in a range (inclusive) WHERE price BETWEEN 10 AND 50 |
| LIKE pattern | Pattern matching (% = any chars, _ = one char) WHERE name LIKE 'J%' |
| IS NULL / IS NOT NULL | Check for null values WHERE deleted_at IS NULL |
| ORDER BY col ASC|DESC | Sort results ORDER BY created_at DESC |
| LIMIT n OFFSET m | Limit number of rows returned LIMIT 10 OFFSET 20 |
Joins
| Command | Description |
|---|---|
| INNER JOIN table ON condition | Return rows with matches in both tables SELECT * FROM orders INNER JOIN users ON orders.user_id = users.id |
| LEFT JOIN table ON condition | Return all rows from left table + matches from right |
| RIGHT JOIN table ON condition | Return all rows from right table + matches from left |
| FULL OUTER JOIN | Return all rows from both tables |
Aggregation
| Command | Description |
|---|---|
| GROUP BY col | Group rows for aggregation SELECT country, COUNT(*) FROM users GROUP BY country |
| HAVING condition | Filter groups (like WHERE but for aggregates) GROUP BY country HAVING COUNT(*) > 10 |
| COUNT(*) | Count number of rows |
| SUM(col) / AVG(col) | Sum or average of a numeric column |
| MIN(col) / MAX(col) | Minimum or maximum value |
Data Modification
| Command | Description |
|---|---|
| INSERT INTO table (cols) VALUES (vals) | Insert a new row INSERT INTO users (name, email) VALUES ('Alice', 'a@b.com') |
| UPDATE table SET col = val WHERE condition | Update existing rows UPDATE users SET name = 'Bob' WHERE id = 1 |
| DELETE FROM table WHERE condition | Delete rows (always use WHERE!) DELETE FROM users WHERE id = 1 |
| UNION / UNION ALL | Combine results from two queries SELECT name FROM users UNION SELECT name FROM admins |
DDL
| Command | Description |
|---|---|
| CREATE TABLE name (cols) | Create a new table CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL) |
| ALTER TABLE name ADD COLUMN col type | Add a column to an existing table ALTER TABLE users ADD COLUMN age INTEGER |
| ALTER TABLE name DROP COLUMN col | Remove a column from a table |
| DROP TABLE name | Delete a table and all its data (irreversible) |
| CREATE INDEX name ON table (col) | Create an index for faster queries CREATE INDEX idx_users_email ON users (email) |
| TRUNCATE TABLE name | Delete all rows quickly (cannot be rolled back in some DBs) |
Frequently Asked Questions
More Cheatsheets
Git Commands Cheatsheet
This cheatsheet covers the 40 most essential Git commands grouped by workflow: setup, staging, branc...
Docker Commands Cheatsheet
This cheatsheet covers 35 essential Docker commands for managing containers, images, volumes, and ne...
Linux Commands Cheatsheet
This cheatsheet covers 40 essential Linux/Unix commands for daily development: file navigation, text...
HTTP Status Codes Cheatsheet
HTTP status codes are 3-digit numbers returned by servers. 1xx = informational, 2xx = success, 3xx =...