SQL Commands

SQL commands cheatsheet.

Query, filter, aggregate, join and modify — the core SQL statements, ready to copy and adapt.

Querying
SELECT * FROM table;Return every column and row from a table
SELECT col1, col2 FROM table;Return only specific columns
SELECT * FROM table WHERE col = 'x';Filter rows by a condition
SELECT DISTINCT col FROM table;Return unique values of a column
SELECT * FROM table ORDER BY col DESC;Sort results by a column
SELECT * FROM table LIMIT 10;Return at most 10 rows
Aggregating
SELECT COUNT(*) FROM table;Count the rows in a table
SELECT col, COUNT(*) FROM table GROUP BY col;Count rows per group
SELECT AVG(col) FROM table;Average a numeric column
SELECT col, SUM(amt) FROM table GROUP BY col HAVING SUM(amt) > 100;Filter groups by an aggregate
Joining
SELECT * FROM a JOIN b ON a.id = b.a_id;Inner join two tables on a key
SELECT * FROM a LEFT JOIN b ON a.id = b.a_id;Keep all rows from the left table
SELECT * FROM a, b WHERE a.id = b.a_id;Older comma-style join syntax
Modifying
INSERT INTO table (a, b) VALUES (1, 'x');Add a new row
UPDATE table SET col = 'x' WHERE id = 1;Change values in matching rows
DELETE FROM table WHERE id = 1;Remove matching rows
CREATE TABLE t (id INT, name TEXT);Create a new table
ALTER TABLE t ADD COLUMN c INT;Add a column to a table
DROP TABLE t;Delete a table and its data
These use standard ANSI SQL syntax; a few details (LIMIT vs TOP, quoting) vary between PostgreSQL, MySQL, SQLite and SQL Server.

Four patterns cover most queries

SQL looks vast, but everyday work leans on four patterns. SELECT … WHERE reads and filters rows; GROUP BY with aggregates like COUNT, SUM and AVG summarises data into totals and averages; JOIN combines rows from related tables; and INSERT, UPDATE and DELETE change data. Recognising which of these a question maps to is most of the battle — "how many orders per customer" is a GROUP BY, "customers with their latest order" is a JOIN.

Joins are where it clicks

The concept that unlocks real queries is the JOIN. An INNER JOIN keeps only rows that match in both tables, while a LEFT JOIN keeps every row from the first table even when there's no match on the right — which is how you find, say, customers who haven't ordered anything. Getting the difference between these two straight resolves a large share of "my query is missing rows" or "my query has too many rows" confusion.

Change data carefully

One habit will save you real pain: an UPDATE or DELETE without a WHERE clause affects every row in the table. Before running either, it's worth writing the matching SELECT … WHERE first to see exactly which rows you'll touch, then converting it. Run inside a transaction when your database supports it, so a mistake can be rolled back. Want cleaner queries to read? Paste them into the SQL formatter to indent and align them.

FAQ

What's the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows with a match in both tables; LEFT JOIN returns all rows from the left table and fills in NULLs where the right table has no match.
How do I limit the number of rows returned?
Use LIMIT 10 in PostgreSQL, MySQL and SQLite. SQL Server uses SELECT TOP 10 … instead.

More cheatsheets

psql commands
PostgreSQL CLI & meta-commands
mysql commands
MySQL client & SHOW