The core of relational databases: querying, filtering, joining, aggregating and changing data, plus the schema and transaction statements you reach for daily. Standard SQL that works across PostgreSQL, MySQL, SQLite and most engines — minor dialect differences aside.
Print or save this page for offline reference.
Querying
Command
Description
SELECT * FROM users;
Return every column and row of a table.
SELECT name, email FROM users;
Return only the named columns.
SELECT DISTINCT country FROM users;
Return unique values, dropping duplicates.
SELECT COUNT(*) FROM users;
Count the number of rows.
Filtering & ordering
Command
Description
WHERE age >= 18
Keep only rows matching a condition.
WHERE name LIKE 'A%'
Pattern match: names starting with A.
WHERE id IN (1,2,3)
Match any value in a list.
ORDER BY created_at DESC
Sort rows, newest first.
LIMIT 10 OFFSET 20
Return 10 rows, skipping the first 20 (paging).
Joins
Command
Description
INNER JOIN orders ON orders.user_id = users.id
Rows with a match in both tables.
LEFT JOIN orders ON ...
All left rows, plus matches (NULLs when none).
COUNT(*) ... GROUP BY user_id
Aggregate rows into groups.
HAVING COUNT(*) > 5
Filter groups after aggregation.
Modifying data
Command
Description
INSERT INTO users (name) VALUES ('Ada');
Add a new row.
UPDATE users SET active = true WHERE id = 1;
Change existing rows — always with WHERE.
DELETE FROM users WHERE id = 1;
Remove rows matching a condition.
UPSERT / ON CONFLICT DO UPDATE
Insert, or update the row if it already exists.
Schema
Command
Description
CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT);