SQL is the language for working with relational databases like PostgreSQL, MySQL, SQLite, and SQL Server. This cheat sheet groups the core SQL statements by task — querying, filtering, joining tables, aggregating, and changing data — so you can find the right syntax fast.
The statements shown here use standard SQL that works across most databases, with only minor dialect differences. Keep this page bookmarked as a quick reference while you write queries.
Querying Data
| Statement | What it does |
|---|
SELECT * FROM table; | Return every column and row from a table. |
SELECT col1, col2 FROM table; | Return only the listed columns. |
SELECT DISTINCT col FROM table; | Return only unique values of a column. |
SELECT * FROM table WHERE condition; | Return only rows matching a condition. |
SELECT * FROM table LIMIT 10; | Return only the first 10 rows. |
SELECT * FROM table ORDER BY col DESC; | Sort the result descending by a column. |
SELECT col AS alias FROM table; | Rename a column in the output. |
Filtering (WHERE)
| Statement | What it does |
|---|
WHERE col = 'value' | Match an exact value. |
WHERE col LIKE 'a%' | Match a pattern — here, values starting with 'a'. |
WHERE col IN (1, 2, 3) | Match any value in a list. |
WHERE col BETWEEN 1 AND 10 | Match an inclusive range. |
WHERE col IS NULL | Match rows where the column has no value. |
WHERE a = 1 AND b = 2 | Combine conditions that must all be true. |
WHERE a = 1 OR b = 2 | Match rows where either condition is true. |
Joining Tables
| Statement | What it does |
|---|
INNER JOIN b ON a.id = b.a_id | Rows with a match in both tables. |
LEFT JOIN b ON a.id = b.a_id | All left-table rows plus matches from the right. |
RIGHT JOIN b ON a.id = b.a_id | All right-table rows plus matches from the left. |
FULL OUTER JOIN b ON a.id = b.a_id | All rows from both tables, matched where possible. |
CROSS JOIN b | Every combination of rows (Cartesian product). |
Aggregation
| Statement | What it does |
|---|
COUNT(*) | Count the number of rows. |
SUM(col) | Add up the values in a column. |
AVG(col) | Average the values in a column. |
MIN(col) / MAX(col) | Smallest / largest value in a column. |
GROUP BY col | Group rows so aggregates apply per group. |
HAVING COUNT(*) > 1 | Filter groups after aggregation. |
Modifying Data
| Statement | What it does |
|---|
INSERT INTO table (a, b) VALUES (1, 2); | Add a new row. |
UPDATE table SET a = 1 WHERE id = 5; | Change values in existing rows. |
DELETE FROM table WHERE id = 5; | Remove rows that match a condition. |
TRUNCATE TABLE table; | Remove all rows quickly, keeping the table. |
Defining Tables
| Statement | What it does |
|---|
CREATE TABLE t (id INT PRIMARY KEY); | Create a new table with a primary key. |
ALTER TABLE t ADD col TYPE; | Add a column to an existing table. |
DROP TABLE t; | Delete a table and all of its data. |
CREATE INDEX idx ON t (col); | Add an index to speed up lookups on a column. |
SQL Commands Cheat Sheet FAQs
What is the difference between WHERE and HAVING?
WHERE filters individual rows before they are grouped. HAVING filters groups after a GROUP BY aggregation. Use WHERE for row conditions and HAVING for conditions on aggregate results like COUNT(*) > 1.
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows that have a match in both tables. LEFT JOIN returns every row from the left table, plus matching rows from the right table, filling in NULLs where there is no match.
What is the difference between DELETE, TRUNCATE, and DROP?
DELETE removes specific rows and can use a WHERE clause. TRUNCATE removes all rows quickly but keeps the table structure. DROP removes the entire table, including its structure and data.
How do I count rows in a SQL table?
Use SELECT COUNT(*) FROM table; for the total number of rows, or add GROUP BY column to count rows per group. Use COUNT(column) to count only rows where that column is not NULL.
What does GROUP BY do in SQL?
GROUP BY collapses rows that share the same value in one or more columns into a single group, so aggregate functions like SUM, AVG, and COUNT return one result per group instead of one for the whole table.
How do I sort SQL query results?
Add ORDER BY column at the end of a query. Use ASC for ascending order (the default) or DESC for descending. You can sort by multiple columns, for example ORDER BY last_name, first_name.
How do I match a pattern in SQL?
Use LIKE with wildcards: % matches any sequence of characters and _ matches a single character. For example, WHERE name LIKE 'A%' matches any name starting with A.