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 minor dialect differences. Keywords are shown in uppercase by convention, but SQL is 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
... WHERE a > 10;Filter rows by a condition
... ORDER BY a DESC;Sort the results
... LIMIT 10 OFFSET 20;Return a page of rows

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 ...All left rows, plus matches (NULLs if none)
RIGHT JOIN ...All right rows, plus matches
FULL JOIN ...All rows from both sides

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

Sources & methodology

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 and data types differ between dialects.

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.