← All cheat sheets

Languages

SQL Cheat Sheet — Query Syntax Reference

The SQL statements and clauses you reach for most, with a one-line explanation of each.

Put it into practice with the matching tool.

Format SQL →

Advertisement

Selecting data

SELECT * FROM users;All columns from a table
SELECT id, name FROM users;Specific columns
SELECT DISTINCT country FROM users;Unique values only
SELECT name AS full_name FROM users;Rename a column in the result
LIMIT 10Return at most 10 rows
ORDER BY created_at DESCSort results (DESC = newest first)

Filtering

WHERE age > 18Basic comparison
WHERE name LIKE 'A%'Pattern match — starts with A
WHERE id IN (1, 2, 3)Match any value in a list
WHERE age BETWEEN 18 AND 30Inclusive range
WHERE email IS NULLTest for NULL (never use = NULL)
WHERE a = 1 AND b = 2Combine conditions (also OR, NOT)

Joins

INNER JOIN orders ON orders.user_id = users.idOnly rows matching in both tables
LEFT JOIN orders ON ...All left rows, plus matches (NULL where none)
RIGHT JOIN orders ON ...All right rows, plus matches
FULL OUTER JOIN orders ON ...All rows from both sides
CROSS JOIN sizesEvery combination of both tables

Aggregation

COUNT(*)Number of rows
SUM(amount) / AVG(amount)Total / average of a column
MIN(x) / MAX(x)Smallest / largest value
GROUP BY countryCollapse rows into groups
HAVING COUNT(*) > 3Filter groups (WHERE filters rows)

Modifying data

INSERT INTO users (name) VALUES ('Ada');Add a row
UPDATE users SET name = 'Ada' WHERE id = 1;Change existing rows
DELETE FROM users WHERE id = 1;Remove rows (always use WHERE)
TRUNCATE TABLE users;Delete every row, fast and unlogged

Tables & indexes

CREATE TABLE users (id INT PRIMARY KEY, name TEXT);Create a table
ALTER TABLE users ADD COLUMN email TEXT;Add a column
DROP TABLE users;Delete a table entirely
CREATE INDEX idx_name ON users(name);Speed up lookups on a column

SQL in practice

The order a query runs in

SQL is written as SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT, but it is evaluated roughly as FROM and JOIN first, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY and finally LIMIT. That explains several common errors: you cannot use a column alias from SELECT in WHERE, because WHERE runs first; you filter groups with HAVING, not WHERE; and ORDER BY can use aliases because it runs after SELECT.

NULL behaves differently

NULL means unknown, and comparisons with it are never true. WHERE discount = NULL returns nothing; write WHERE discount IS NULL. WHERE status <> 'cancelled' silently excludes rows where status is NULL. Aggregates such as COUNT(column) and AVG(column) ignore NULLs, while COUNT(*) counts every row. Use COALESCE(discount, 0) to substitute a default when NULL should behave like a value.

Joins without surprises

Always join on the correct keys and check row counts. A missing join condition creates a cross join that multiplies rows. Joining two one-to-many tables at once — customers to orders and to addresses — multiplies rows and inflates sums; aggregate each child table separately first. With a LEFT JOIN, put conditions on the right-hand table in the ON clause, not in WHERE, or the unmatched rows disappear.

Writing safe data changes

Before running an UPDATE or DELETE, run the same WHERE clause as a SELECT and check the rows it returns. Wrap risky changes in a transaction — BEGIN, the statement, a check, then COMMIT or ROLLBACK. An UPDATE without a WHERE clause changes every row in the table. In application code, always pass values as query parameters rather than concatenating them into SQL, which prevents SQL injection.

Making queries fast

Index the columns used to filter, join and sort, especially foreign keys. Select only the columns you need instead of SELECT *. Avoid wrapping indexed columns in functions in WHERE — WHERE DATE(created_at) = '2025-01-31' cannot use a plain index on created_at, while a range condition can. Use EXPLAIN to see whether the database scans the whole table, and test with realistic data volumes, since a query that is instant on a hundred rows can crawl on ten million.

Related guides

More cheat sheets