SQL Cheat Sheet
Last updated
Querying data (SELECT)
The core of SQL: read rows from one or more tables.
| Operation | Syntax |
|---|---|
| Select all columns | SELECT * FROM users; |
| Select specific columns | SELECT id, name FROM users; |
| Rename a column (alias) | SELECT name AS full_name FROM users; |
| Distinct values | SELECT DISTINCT country FROM users; |
| Limit rows | SELECT * FROM users LIMIT 10; |
| Skip then limit (paging) | SELECT * FROM users LIMIT 10 OFFSET 20; |
| Count rows | SELECT COUNT(*) FROM users; |
Filtering rows (WHERE)
Narrow down which rows a query returns.
| Operation | Syntax |
|---|---|
| Equality / comparison | WHERE age >= 18 |
| Multiple conditions | WHERE age >= 18 AND country = 'US' |
| Either condition | WHERE role = 'admin' OR role = 'owner' |
| In a set | WHERE country IN ('US', 'CA', 'MX') |
| Between a range | WHERE price BETWEEN 10 AND 50 |
| Pattern match | WHERE email LIKE '%@gmail.com' |
| Is / is not null | WHERE deleted_at IS NULL |
| Negate | WHERE status NOT IN ('archived') |
Sorting & grouping
| Operation | Syntax |
|---|---|
| Sort ascending (default) | ORDER BY created_at |
| Sort descending | ORDER BY created_at DESC |
| Sort by multiple columns | ORDER BY country, name DESC |
| Group rows | SELECT country, COUNT(*) FROM users GROUP BY country; |
| Filter groups | GROUP BY country HAVING COUNT(*) > 100 |
Joins
Combine rows from two tables on a matching column.
| Join type | Returns | Syntax |
|---|---|---|
| INNER JOIN | Only rows that match in both tables | FROM orders o INNER JOIN users u ON o.user_id = u.id |
| LEFT JOIN | All left rows + matches (nulls otherwise) | FROM users u LEFT JOIN orders o ON o.user_id = u.id |
| RIGHT JOIN | All right rows + matches | FROM orders o RIGHT JOIN users u ON o.user_id = u.id |
| FULL OUTER JOIN | All rows from both sides | FROM a FULL OUTER JOIN b ON a.id = b.a_id |
| CROSS JOIN | Every combination (cartesian) | FROM sizes CROSS JOIN colors |
| Self join | A table joined to itself | FROM employees e JOIN employees m ON e.manager_id = m.id |
Aggregate functions
Compute a single value across a group of rows.
| Function | What it does |
|---|---|
COUNT(*) | Number of rows |
COUNT(col) | Number of non-null values |
SUM(col) | Total of a numeric column |
AVG(col) | Average value |
MIN(col) / MAX(col) | Smallest / largest value |
STRING_AGG(col, ',') | Concatenate values (Postgres; GROUP_CONCAT in MySQL) |
Window functions
Compute across a set of rows without collapsing them into one.
| Function | Syntax |
|---|---|
| Row number | ROW_NUMBER() OVER (ORDER BY score DESC) |
| Rank (with gaps) | RANK() OVER (PARTITION BY country ORDER BY score DESC) |
| Dense rank | DENSE_RANK() OVER (ORDER BY score DESC) |
| Running total | SUM(amount) OVER (ORDER BY created_at) |
| Previous / next row | LAG(score) OVER (ORDER BY date), LEAD(score) OVER (...) |
Changing data (INSERT, UPDATE, DELETE)
| Operation | Syntax |
|---|---|
| Insert a row | INSERT INTO users (name, email) VALUES ('Ada', 'ada@x.com'); |
| Insert multiple rows | INSERT INTO users (name) VALUES ('Ada'), ('Alan'); |
| Update rows | UPDATE users SET active = true WHERE id = 1; |
| Delete rows | DELETE FROM users WHERE active = false; |
| Upsert (Postgres/SQLite) | INSERT ... ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name; |
Defining tables (DDL)
| Operation | Syntax |
|---|---|
| Create a table | CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL); |
| Add a column | ALTER TABLE users ADD COLUMN age INTEGER; |
| Drop a column | ALTER TABLE users DROP COLUMN age; |
| Drop a table | DROP TABLE users; |
| Create an index | CREATE INDEX idx_users_email ON users (email); |
| Foreign key | FOREIGN KEY (user_id) REFERENCES users (id) |
Every SQL statement, clause, and join you reach for, on one page. This SQL cheat sheet is a quick reference for querying and changing relational data - the SELECT clauses, the four JOIN types, aggregation, and the DDL and DML you use to create and update tables.
The syntax here is standard ANSI SQL and works in PostgreSQL, MySQL, SQLite, and SQL Server unless noted. Copy what you need, or try every query live in the SQL playground - no database to install.
SQL cheat sheet FAQ
Is this SQL cheat sheet free?
Does this work for MySQL, PostgreSQL, and SQLite?
SELECT, WHERE, JOIN, GROUP BY, and DDL syntax shown here is standard ANSI SQL and runs the same in MySQL, PostgreSQL, SQLite, and SQL Server. A few advanced features differ by dialect - upserts and string aggregation are the common ones - and those rows note the difference.What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only the rows that have a match in both tables. A LEFT JOIN returns every row from the left (first) table, filling in NULL for columns from the right table when there is no match - useful when you want all users even if some have no orders.Can I practice these SQL commands online?
SELECT to joins and window functions step by step.