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 tableSELECT id, name FROM users;Specific columnsSELECT DISTINCT country FROM users;Unique values onlySELECT name AS full_name FROM users;Rename a column in the resultLIMIT 10Return at most 10 rowsORDER BY created_at DESCSort results (DESC = newest first)Filtering
WHERE age > 18Basic comparisonWHERE name LIKE 'A%'Pattern match — starts with AWHERE id IN (1, 2, 3)Match any value in a listWHERE age BETWEEN 18 AND 30Inclusive rangeWHERE 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 tablesLEFT JOIN orders ON ...All left rows, plus matches (NULL where none)RIGHT JOIN orders ON ...All right rows, plus matchesFULL OUTER JOIN orders ON ...All rows from both sidesCROSS JOIN sizesEvery combination of both tablesAggregation
COUNT(*)Number of rowsSUM(amount) / AVG(amount)Total / average of a columnMIN(x) / MAX(x)Smallest / largest valueGROUP BY countryCollapse rows into groupsHAVING COUNT(*) > 3Filter groups (WHERE filters rows)Modifying data
INSERT INTO users (name) VALUES ('Ada');Add a rowUPDATE users SET name = 'Ada' WHERE id = 1;Change existing rowsDELETE FROM users WHERE id = 1;Remove rows (always use WHERE)TRUNCATE TABLE users;Delete every row, fast and unloggedTables & indexes
CREATE TABLE users (id INT PRIMARY KEY, name TEXT);Create a tableALTER TABLE users ADD COLUMN email TEXT;Add a columnDROP TABLE users;Delete a table entirelyCREATE INDEX idx_name ON users(name);Speed up lookups on a columnSQL 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.

