SQL Cheat Sheet

A reference for standard SQL, querying with SELECT, filtering with WHERE, aggregates and GROUP BY, joining tables, and modifying data and schema.

A reference for standard SQL, the syntax is shared across PostgreSQL, MySQL, SQLite, and SQL Server, with dialect differences noted below. Keywords are shown in uppercase by convention; SQL keywords are case-insensitive.

Querying data

StatementWhat it does
SELECT * FROM t;Return every column and row
SELECT a, b FROM t;Return specific columns
SELECT DISTINCT a FROM t;Return unique values
SELECT * FROM t WHERE a > 10;Filter rows by a condition
SELECT * FROM t ORDER BY a DESC;Sort the results
SELECT * FROM t LIMIT 10 OFFSET 20;Return a page of rows (PostgreSQL, MySQL, SQLite; not SQL Server)
SELECT * FROM t ORDER BY a OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;Standard paging; works in SQL Server and PostgreSQL. SQL Server also has SELECT TOP (10) * FROM t;

Filtering (WHERE)

OperatorMatches
= <> < >Equal, not equal, less / greater than
BETWEEN 1 AND 9Within an inclusive range
IN ('a','b')Matches any value in a list
LIKE 'a%'Pattern match (% = any, _ = one char)
IS NULLField has no value
AND / OR / NOTCombine conditions

Aggregates & grouping

Function / clauseWhat it does
COUNT(*)Number of rows
SUM(a) AVG(a)Total / average of a column
MIN(a) MAX(a)Smallest / largest value
GROUP BY aAggregate per distinct value of a
HAVING COUNT(*) > 1Filter groups (like WHERE, but post-aggregate)

Joining tables

JoinReturns
INNER JOIN u ON u.id = t.uidOnly rows matching in both tables
LEFT JOIN u ON u.id = t.uidAll left rows, plus matches (NULLs if none)
RIGHT JOIN u ON u.id = t.uidAll right rows, plus matches (SQLite 3.39.0+)
FULL JOIN u ON u.id = t.uidAll rows from both sides, NULLs where unmatched. Not in MySQL (combine a LEFT and a RIGHT JOIN with UNION); SQLite 3.39.0+

Modifying data & tables

StatementWhat it does
INSERT INTO t (a,b) VALUES (1,2);Add a new row
UPDATE t SET a=1 WHERE id=5;Change existing rows
DELETE FROM t WHERE id=5;Remove rows
CREATE TABLE t (id INT PRIMARY KEY, a TEXT);Create a table
ALTER TABLE t ADD COLUMN c INT;Add a column. SQL Server has no COLUMN keyword: ALTER TABLE t ADD c INT;
CREATE INDEX idx ON t (a);Speed up lookups on a column

Always pair UPDATE and DELETE with a WHERE clause, without one they affect every row. Test with a SELECT using the same WHERE first.

What this does

A SQL cheat sheet covers standard SQL — querying with SELECT, filtering with WHERE, aggregates and GROUP BY, joining tables, and modifying data.

How to use it

  1. Browse by clause.
  2. Find the syntax you need.
  3. Copy it with one tap.
  4. Swap in your table and column names.

Example

SELECT * FROM users WHERE active = 1 returns every active user (with active stored as a 0/1 number).

Last updated .

Frequently asked questions

Does this apply to MySQL and PostgreSQL?

Yes, the core syntax is shared across PostgreSQL, MySQL, SQLite, and SQL Server. Some functions, data types and clauses differ: SQL Server pages with TOP or OFFSET/FETCH instead of LIMIT, MySQL has no FULL JOIN, and the sheet notes each of these.

What’s the difference between WHERE and HAVING?

WHERE filters individual rows before grouping; HAVING filters groups after aggregation. Use HAVING with aggregate conditions like COUNT(*) > 1.

How do I avoid deleting every row?

Always pair UPDATE and DELETE with a WHERE clause. Test the same WHERE with a SELECT first to confirm exactly which rows are affected.