This SQL cheat sheet brings together the syntax you use every day: reading and filtering data, summarising it, joining tables, writing subqueries and changing the structure. Each row shows what the statement is for, its general form and a runnable example on the fictional StoreSQL (customers, products, orders, order_items, categories, employees and payments).
The examples were run in SQLite. Most work the same way in PostgreSQL, MySQL, SQL Server and Oracle; the last section sums up the most common differences. If you are just starting out, the cheat sheet does not replace the SQL course: it is there to remind you of a statement's form once you already understand what it does.
Query and filter data
For…
Syntax
Example in StoreSQL
Choose columns
SELECT col1, col2 FROM table_name;
SELECT name, city FROM customers;
All columns
SELECT * FROM table_name;
SELECT * FROM products;
Give an alias
SELECT col AS alias FROM table_name;
SELECT price * stock AS stock_value FROM products;
Remove duplicates
SELECT DISTINCT col FROM table_name;
SELECT DISTINCT city FROM customers;
Filter rows
WHERE condición
SELECT name FROM customers WHERE city = 'London' AND active = 1;
Range of values
col BETWEEN a AND b
SELECT name, price FROM products WHERE price BETWEEN 20 AND 100;
List of values
col IN (a, b, c)
SELECT name FROM customers WHERE city IN ('Bristol', 'Leeds');
Text pattern
col LIKE 'pattern'
SELECT name FROM products WHERE name LIKE 'Monitor%';
Missing values
col IS NULL / IS NOT NULL
SELECT name FROM customers WHERE email IS NULL;
Tidy up
ORDER BY col [ASC|DESC]
SELECT name, price FROM products ORDER BY price DESC, name;
Limit rows
LIMIT n [OFFSET m]
SELECT name, price FROM products ORDER BY price DESC LIMIT 3;
Conditional value
CASE WHEN … THEN … ELSE … END
SELECT name, CASE WHEN stock = 0 THEN 'out_of_stock' ELSE 'available' END AS situation FROM products;
Replace NULL
COALESCE(col, value)
SELECT name, COALESCE(email, 'no email') AS email FROM customers;
Summarise and group data
For…
Syntax
Example in StoreSQL
Count rows
COUNT(*)
SELECT COUNT(*) AS orders FROM orders;
Count values that are present
COUNT(col) ignores NULLs
SELECT COUNT(email) AS with_email FROM customers;
Add up and average
SUM(col), AVG(col)
SELECT SUM(total) AS amount, ROUND(AVG(total), 2) AS average_ticket FROM orders;
Minimum and maximum
MIN(col), MAX(col)
SELECT MIN(date) AS first, MAX(date) AS last FROM orders;
Summarise by group
GROUP BY col
SELECT status, COUNT(*) AS orders FROM orders GROUP BY status;
Filter groups
HAVING condición_agregada
SELECT city, COUNT(*) AS customers FROM customers GROUP BY city HAVING COUNT(*) >= 3;
Count distinct
COUNT(DISTINCT col)
SELECT COUNT(DISTINCT customer_id) AS customers_with_orders FROM orders;
Combine tables with JOIN
For…
Syntax
Example in StoreSQL
Matches only
table_a JOIN table_b ON a.col = b.col
SELECT c.name, p.total FROM customers c JOIN orders p ON p.customer_id = c.id;
Keep every row on the left
LEFT JOIN … ON …
SELECT c.name, p.id AS order_record FROM customers c LEFT JOIN orders p ON p.customer_id = c.id;
Rows with no match
LEFT JOIN … WHERE b.id IS NULL
SELECT c.name FROM customers c LEFT JOIN orders p ON p.customer_id = c.id WHERE p.id IS NULL;
Join three tables
JOIN … JOIN …
SELECT p.id, pr.name, d.quantity FROM orders p JOIN order_items d ON d.order_id = p.id JOIN products pr ON pr.id = d.product_id;
A table joined to itself
table_name a JOIN table_name b ON b.id = a.ref
SELECT e.name AS employee, r.name AS owner FROM employees e LEFT JOIN employees r ON r.id = e.manager_id;
Stack results
consulta1 UNION [ALL] consulta2
SELECT name FROM customers WHERE city = 'London' UNION SELECT name FROM customers WHERE active = 0;
Subqueries, CTEs and window functions
For…
Syntax
Example in StoreSQL
Subquery as value
WHERE col > (SELECT …)
SELECT id, total FROM orders WHERE total > (SELECT AVG(total) FROM orders);
Existence
WHERE EXISTS (SELECT 1 …)
SELECT c.name FROM customers c WHERE EXISTS (SELECT 1 FROM orders p WHERE p.customer_id = c.id);
Named query (CTE)
WITH name AS (SELECT …) SELECT … FROM name
WITH by_customer AS (SELECT customer_id, SUM(total) AS amount FROM orders GROUP BY customer_id) SELECT customer_id, amount FROM by_customer WHERE amount > 200;
Number rows
ROW_NUMBER() OVER (ORDER BY col)
SELECT name, price, ROW_NUMBER() OVER (ORDER BY price DESC) AS position FROM products;
Number per group
… OVER (PARTITION BY g ORDER BY col)
SELECT category, name, RANK() OVER (PARTITION BY category ORDER BY price DESC) AS position FROM products;
Running total
SUM(col) OVER (ORDER BY col2)
SELECT date, total, SUM(total) OVER (ORDER BY date, id) AS cumulative FROM orders;
Insert, update and delete data
For…
Syntax
Example in StoreSQL
Insert a row
INSERT INTO t (cols) VALUES (values);
INSERT INTO categories (id, name) VALUES (6, 'Networks');
Insert from a query
INSERT INTO t (cols) SELECT …;
INSERT INTO categories (id, name) SELECT 7, 'Printing';
Update rows
UPDATE t SET col = value WHERE …;
UPDATE products SET stock = stock + 10 WHERE name = 'Keyboard';
Delete rows
DELETE FROM t WHERE …;
DELETE FROM orders WHERE status = 'cancelled' AND id NOT IN (SELECT order_id FROM order_items);
Group changes together
BEGIN; …; COMMIT; or ROLLBACK;
BEGIN; UPDATE products SET stock = 0 WHERE id = 4; ROLLBACK;
Create and change tables
For…
Syntax
Example in StoreSQL
Create a table
CREATE TABLE t (col type constraints);
CREATE TABLE labels (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);
Foreign key
FOREIGN KEY (col) REFERENCES other(id)
CREATE TABLE reviews (id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL, text_value TEXT, FOREIGN KEY (product_id) REFERENCES products(id));
Add a column
ALTER TABLE t ADD COLUMN col type;
ALTER TABLE customers ADD COLUMN phone TEXT;
Create an index
CREATE INDEX name ON t (col);
CREATE INDEX idx_orders_customer ON orders (customer_id);
Creating a view
CREATE VIEW v AS SELECT …;
CREATE VIEW shipped_orders AS SELECT id, customer_id, total FROM orders WHERE status = 'shipped';
Drop a table
DROP TABLE t;
CREATE TABLE temporary (id INTEGER); DROP TABLE temporary;
Common differences between engines
These are the differences that most often break a query when changing databases. Always check the documentation for the version you use.