← back to databases

SQL Basics

Wikipedia · wpSQL · 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.

SQL SELECT name FROM users WHERE age > 25 ORDER BY name compiles to Relational Algebra sort project(name) select(age > 25) scan(users) SQL compiles to relational algebra operations.

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.

Python

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.

Python

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.

Python

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.

Python

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.

Scheme
Neighbors

Foundations (Wikipedia)