SQL
SQL is the language for asking questions of a relational database. A handful of keywords covers most of what you will ever write.
SQL (Structured Query Language) is how you read and change data in relational databases like PostgreSQL, MySQL, and SQLite. Data lives in tables — rows and columns — and SQL describes what you want, not how to fetch it.
Reading data
SELECT name, email
FROM users
WHERE active = true
ORDER BY name
LIMIT 10;
Read it as: pick these columns, from this table, keep rows matching this condition, sort them, and stop after ten.
Filtering
WHERE age >= 18
WHERE name LIKE 'A%' -- starts with A
WHERE country IN ('US', 'UK')
WHERE deleted_at IS NULL
Aggregating
SELECT country, COUNT(*) AS people
FROM users
GROUP BY country
HAVING COUNT(*) > 100;
GROUP BY collapses rows into groups; aggregate functions like COUNT, SUM, AVG summarize each group. HAVING filters the groups (WHERE filters rows before grouping).
Joining tables
Relational data is split across tables and reconnected with joins:
SELECT orders.id, users.name
FROM orders
JOIN users ON users.id = orders.user_id;
An INNER JOIN keeps only matching rows; a LEFT JOIN keeps every row from the left table even when there is no match.
Changing data
INSERT INTO users (name, email) VALUES ('Ada', '[email protected]');
UPDATE users SET active = false WHERE id = 42;
DELETE FROM users WHERE id = 42;
UPDATE and DELETE without a WHERE clause hit every row. Run a SELECT with the same condition first to see what you are about to touch.