Home Tools Blog About

SQL Cheat Sheet (Common Queries)

SQL is the language for querying and changing data in relational databases. This cheat sheet covers the statements you use most, from basic queries to joins, aggregates, and updates.

Query basics

Statement What it does
SELECT * FROM t Get all columns from a table
SELECT a, b FROM t Get specific columns
WHERE x = 1 Filter rows by a condition
ORDER BY a DESC Sort results, descending
LIMIT 10 Return only the first 10 rows
SELECT DISTINCT a FROM t Remove duplicate values

Filtering

Operator What it does
a = 1 / a <> 1 Equal / not equal
a IN (1,2,3) Matches any value in a list
a BETWEEN 1 AND 9 Within a range
a LIKE 'A%' Pattern match; % is any text
a IS NULL Is empty (null)
AND / OR Combine conditions

Joins

Join What it does
INNER JOIN Rows matching in both tables
LEFT JOIN All left rows, matched right or null
RIGHT JOIN All right rows, matched left or null
FULL JOIN All rows from both sides
ON a.id = b.id The condition that links the tables

Aggregates

Function What it does
COUNT(*) Count rows
SUM(a) Add up a column
AVG(a) Average of a column
MIN(a) / MAX(a) Smallest / largest value
GROUP BY a Group rows for aggregation
HAVING COUNT(*) > 1 Filter groups after aggregating

Modify data

Statement What it does
INSERT INTO t (a) VALUES (1) Add a new row
UPDATE t SET a = 1 WHERE id = 2 Change existing rows
DELETE FROM t WHERE id = 2 Remove rows
CREATE TABLE t (id INT, a TEXT) Create a table
ALTER TABLE t ADD b INT Add a column
DROP TABLE t Delete a table

Frequently Asked Questions

What is SQL?

SQL, or Structured Query Language, is the standard language for reading and writing data in relational databases like MySQL, PostgreSQL, and SQLite.

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping. HAVING filters groups after an aggregate like COUNT or SUM has been applied.

What is the difference between INNER and LEFT JOIN?

An inner join returns only rows that match in both tables. A left join returns all rows from the left table, with nulls where there is no match.

Is SQL the same in every database?

The core is standardized, so most queries are portable. Each database adds its own extensions and small differences in functions and types.

See our free developer tools for formatting and data work.

ATV

Written by Nick (ATV Team)

We build and maintain the 600+ free, client-side tools on this site, and every guide is written against the tools themselves: each figure is computed and checked before it is published, and every linked tool is tested in the browser. More about how we work on the about page, and the full library of guides lives on the blog.