Skip to main content
← All cheat sheets
πŸ—„οΈ

SQL Cheat Sheet

A quick-reference SQL cheat sheet covering SELECT, WHERE, JOINs, GROUP BY, aggregates, and more.

Querying

SELECT * FROM users;Select every column from a table.
SELECT name, age FROM users;Select only specific columns.
SELECT name AS full_name FROM users;Rename a column in the output using an alias.
SELECT DISTINCT country FROM users;Return only unique values for a column.
SELECT * FROM users LIMIT 10;Return at most the first 10 rows.
SELECT * FROM users LIMIT 10 OFFSET 20;Skip 20 rows, then return the next 10 (pagination).

Filtering

SELECT * FROM users WHERE age > 18;Keep only rows matching a condition.
SELECT * FROM users WHERE age >= 18 AND country = 'US';Combine conditions that must all be true.
SELECT * FROM users WHERE country = 'US' OR country = 'UK';Match rows where either condition is true.
SELECT * FROM users WHERE country IN ('US', 'UK', 'CA');Match any value in a list.
SELECT * FROM users WHERE age BETWEEN 18 AND 65;Match values within an inclusive range.
SELECT * FROM users WHERE name LIKE 'A%';Pattern match; % is any string, _ is one character.
SELECT * FROM users WHERE email IS NULL;Match rows where a value is missing.
SELECT * FROM users WHERE email IS NOT NULL;Match rows where a value is present.

Sorting

SELECT * FROM users ORDER BY age;Sort rows in ascending order (default).
SELECT * FROM users ORDER BY age DESC;Sort rows in descending order.
SELECT * FROM users ORDER BY country, age DESC;Sort by multiple columns in priority order.
SELECT * FROM users ORDER BY age DESC LIMIT 5;Get the top 5 rows by a sorted column.

Aggregates

SELECT COUNT(*) FROM users;Count the number of rows.
SELECT COUNT(DISTINCT country) FROM users;Count distinct non-null values.
SELECT SUM(amount) FROM orders;Add up all values in a column.
SELECT AVG(age) FROM users;Compute the average of a column.
SELECT MIN(price) FROM products;Find the smallest value.
SELECT MAX(price) FROM products;Find the largest value.

Grouping

SELECT country, COUNT(*) FROM users GROUP BY country;Aggregate rows into groups by a column.
SELECT country, AVG(age) FROM users GROUP BY country;Apply an aggregate per group.
SELECT country, COUNT(*) FROM users GROUP BY country HAVING COUNT(*) > 100;Filter groups after aggregation with HAVING.
SELECT country, COUNT(*) AS n FROM users GROUP BY country ORDER BY n DESC;Group, then sort by the aggregate result.

Joins

SELECT * FROM orders JOIN users ON orders.user_id = users.id;Inner join: rows with matches in both tables.
SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id;Left join: all left rows, matched right rows or NULLs.
SELECT * FROM orders RIGHT JOIN users ON orders.user_id = users.id;Right join: all right rows, matched left rows or NULLs.
SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id;Use table aliases to keep joins readable.
SELECT * FROM a CROSS JOIN b;Cross join: every combination of rows.

Modifying Data

INSERT INTO users (name, age) VALUES ('Ada', 36);Insert a single new row.
INSERT INTO users (name, age) VALUES ('Ada', 36), ('Grace', 40);Insert multiple rows at once.
UPDATE users SET age = 37 WHERE name = 'Ada';Update matching rows; always use WHERE.
DELETE FROM users WHERE id = 5;Delete matching rows.
DELETE FROM users;Delete all rows in a table (no WHERE).

Tables

CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER);Create a table with column definitions.
CREATE TABLE IF NOT EXISTS logs (msg TEXT);Create a table only if it does not already exist.
ALTER TABLE users ADD COLUMN email TEXT;Add a new column to an existing table.
CREATE INDEX idx_users_name ON users (name);Create an index to speed up lookups.
DROP TABLE users;Delete a table and all its data.
DROP TABLE IF EXISTS users;Drop a table only if it exists.

Reading is good β€” doing is better. Practice SQL with interactive, auto-graded lessons.

Start learning SQL