DD
DevDash

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

CommandDescription
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

CommandDescription
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

CommandDescription
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

CommandDescription
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

CommandDescription
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

Want API access + no ads? Pro coming soon.