SQL Basics
Wikipedia · SQL · CC BY-SA 4.0
SQL (Structured Query Language) is the standard language for relational databases. It maps directly to relational algebra: SELECT is projection, WHERE is selection, JOIN is join, UNION is union. SQL adds aggregation (GROUP BY, COUNT, SUM) and ordering (ORDER BY) on top of the algebra.
CREATE and INSERT — defining and populating tables
CREATE TABLE defines a schema: column names, types, and constraints. INSERT adds tuples. We simulate this in Python with sqlite3, which runs entirely in memory.
SELECT and WHERE — querying data
SELECT chooses which columns to return (projection). WHERE filters which rows to return (selection). Together they implement the two most basic relational algebra operations.
JOIN — combining tables
JOIN combines rows from two tables based on a related column. INNER JOIN returns only matching rows. LEFT JOIN returns all rows from the left table, with NULLs where the right table has no match.
GROUP BY and aggregate functions
GROUP BY partitions rows into groups. Aggregate functions (COUNT, SUM, AVG, MIN, MAX) compute a single value per group. HAVING filters groups after aggregation, the way WHERE filters rows before aggregation.
ORDER BY — sorting results
ORDER BY sorts the output. It is not a relational algebra operation (relations are unordered sets), but SQL adds it as a convenience. ASC for ascending (default), DESC for descending.