Menu

SQL Cheat Sheet

Last updated

Querying data (SELECT)

The core of SQL: read rows from one or more tables.

OperationSyntax
Select all columnsSELECT * FROM users;
Select specific columnsSELECT id, name FROM users;
Rename a column (alias)SELECT name AS full_name FROM users;
Distinct valuesSELECT DISTINCT country FROM users;
Limit rowsSELECT * FROM users LIMIT 10;
Skip then limit (paging)SELECT * FROM users LIMIT 10 OFFSET 20;
Count rowsSELECT COUNT(*) FROM users;

Filtering rows (WHERE)

Narrow down which rows a query returns.

OperationSyntax
Equality / comparisonWHERE age >= 18
Multiple conditionsWHERE age >= 18 AND country = 'US'
Either conditionWHERE role = 'admin' OR role = 'owner'
In a setWHERE country IN ('US', 'CA', 'MX')
Between a rangeWHERE price BETWEEN 10 AND 50
Pattern matchWHERE email LIKE '%@gmail.com'
Is / is not nullWHERE deleted_at IS NULL
NegateWHERE status NOT IN ('archived')

Sorting & grouping

OperationSyntax
Sort ascending (default)ORDER BY created_at
Sort descendingORDER BY created_at DESC
Sort by multiple columnsORDER BY country, name DESC
Group rowsSELECT country, COUNT(*) FROM users GROUP BY country;
Filter groupsGROUP BY country HAVING COUNT(*) > 100

Joins

Combine rows from two tables on a matching column.

Join typeReturnsSyntax
INNER JOINOnly rows that match in both tablesFROM orders o INNER JOIN users u ON o.user_id = u.id
LEFT JOINAll left rows + matches (nulls otherwise)FROM users u LEFT JOIN orders o ON o.user_id = u.id
RIGHT JOINAll right rows + matchesFROM orders o RIGHT JOIN users u ON o.user_id = u.id
FULL OUTER JOINAll rows from both sidesFROM a FULL OUTER JOIN b ON a.id = b.a_id
CROSS JOINEvery combination (cartesian)FROM sizes CROSS JOIN colors
Self joinA table joined to itselfFROM employees e JOIN employees m ON e.manager_id = m.id

Aggregate functions

Compute a single value across a group of rows.

FunctionWhat 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.

FunctionSyntax
Row numberROW_NUMBER() OVER (ORDER BY score DESC)
Rank (with gaps)RANK() OVER (PARTITION BY country ORDER BY score DESC)
Dense rankDENSE_RANK() OVER (ORDER BY score DESC)
Running totalSUM(amount) OVER (ORDER BY created_at)
Previous / next rowLAG(score) OVER (ORDER BY date), LEAD(score) OVER (...)

Changing data (INSERT, UPDATE, DELETE)

OperationSyntax
Insert a rowINSERT INTO users (name, email) VALUES ('Ada', 'ada@x.com');
Insert multiple rowsINSERT INTO users (name) VALUES ('Ada'), ('Alan');
Update rowsUPDATE users SET active = true WHERE id = 1;
Delete rowsDELETE FROM users WHERE active = false;
Upsert (Postgres/SQLite)INSERT ... ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;

Defining tables (DDL)

OperationSyntax
Create a tableCREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
Add a columnALTER TABLE users ADD COLUMN age INTEGER;
Drop a columnALTER TABLE users DROP COLUMN age;
Drop a tableDROP TABLE users;
Create an indexCREATE INDEX idx_users_email ON users (email);
Foreign keyFOREIGN 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?
Yes. This SQL cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up a clause, join, or function.
Does this work for MySQL, PostgreSQL, and SQLite?
Mostly, yes. The 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?
An 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?
Yes. Open the SQL playground to run any query from this cheat sheet against a real database in your browser - no install needed. When you want structure, Coddy's free interactive SQL course takes you from SELECT to joins and window functions step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from the most common operations (querying and filtering) down to advanced ones (window functions), so you can use the top sections on day one and grow into the rest.
Coddy programming languages illustration

Learn SQL with Coddy

GET STARTED