Query, build, and manage databases from Python with SQLAlchemy Core and the ORM — engines, tables, joins, transactions, and a real census data project.
Welcome to SQLAlchemy for Python — the module where everything you have learned about SQL becomes a tool you can call from your own Python programs. You already know how to write a SELECT, join tables, and query a database from a script with sqlite3. SQLAlchemy takes that a step further: it lets you describe tables as Python objects, build queries with Python method calls instead of query strings, and — when you want it — map database rows onto full Python classes through its Object-Relational Mapper (the ORM).
Data for this module
Databases: employees.db — a small staff table you will reflect in Lesson 1, and company.db — the departments and employees tables you build yourself in Lesson 2 and reuse through Lesson 7 (a ready-made copy is provided so you can jump to any lesson).
CSV: census.csv — U.S. Census population by state, sex, and age (2000 and 2008), used in the Lesson 8 guided project.
Quick reference: SQLAlchemy 2.0 Quick Reference (PDF) — a one-page cheat sheet covering every pattern in this module, worth keeping open while you work.
Each lesson also lists the exact tables it uses.
You will start with SQLAlchemy Core — the layer that lets you define tables with Table() and Column(), and build select(), insert(), update(), and delete() statements without writing raw SQL strings. From there you will connect two tables with a foreign key, write joins and self-joins, and wrap changes in transactions so a failure partway through never leaves your data half-updated. Then you will meet the ORM, where those same tables become Python classes with attributes and relationships you can navigate like objects — employee.department.name instead of a second query. The module closes with pagination, bulk operations, and indexing for when your tables grow large, then ties everything together in a guided project that loads real U.S. Census data, aggregates it with SQLAlchemy, and visualizes the result.
Every lesson runs against a real SQLite database, and every number in this module’s examples and figures comes from that data — nothing is fabricated. You will need Python and the concepts from Querying Databases from Python earlier in this course; install SQLAlchemy with pip install sqlalchemy (version 2.0 or later) before you begin. Start with Lesson 1, where you will connect to a database and inspect its tables without writing a single line of SQL.
Complete all 8 lessons to finish the SQLAlchemy for Python module.