Skip to main content

Welcome to SQLAlchemy Tutorials

SQLAlchemy is presented as two distinct APIs: Core and ORM. These tutorials will guide you through both approaches, helping you become proficient with Python’s most powerful database toolkit.

About SQLAlchemy

SQLAlchemy provides two primary ways to work with databases:

SQLAlchemy Core

The foundational architecture providing database connectivity, query execution, and programmatic SQL construction.

SQLAlchemy ORM

Builds on Core to provide object-relational mapping, allowing Python classes to be mapped to database tables.

Learning Path

Follow these tutorials in sequence for the best learning experience:
1

Unified Tutorial

Start with the Unified Tutorial to understand both Core and ORM fundamentals in an integrated way.
2

Data Manipulation

Learn how to insert, update, and delete data using the ORM.
3

Querying Data

Master querying with select(), filter(), and joins to retrieve exactly the data you need.
4

Working with Relationships

Understand relationship patterns and loading strategies for complex data models.

Tutorial Structure

Core vs ORM

Throughout these tutorials, you’ll learn:
  • Core-focused sections: Work directly with SQL constructs, tables, and connections
  • ORM-focused sections: Use Python classes and sessions for object persistence
  • Integrated sections: See how Core and ORM work together in SQLAlchemy 2.0
SQLAlchemy 2.0 features deep integration between Core and ORM. The ORM now uses Core-style querying with the select() construct, making the APIs more unified than ever.

What You’ll Learn

By the end of these tutorials, you’ll be able to:

Define Models

Create declarative ORM models with typed attributes using Mapped and mapped_column

Execute Queries

Build complex SELECT queries with filtering, ordering, joining, and grouping

Manage Relationships

Configure one-to-many, many-to-one, and many-to-many relationships with proper loading strategies

Handle Transactions

Work with sessions, transactions, and understand the Unit of Work pattern

Prerequisites

Before starting, you should have:
  • Basic Python knowledge (classes, functions, type hints)
  • Understanding of relational database concepts (tables, rows, foreign keys)
  • Python 3.7+ installed
  • SQLAlchemy installed: pip install sqlalchemy
These tutorials use Python type hints extensively, which is the recommended approach for modern SQLAlchemy 2.0+ code.

Code Examples

All examples in these tutorials are:
  • Runnable: Copy and execute them directly
  • Complete: No missing imports or setup code
  • Modern: Follow SQLAlchemy 2.0+ best practices
  • Typed: Use Python type hints for better IDE support

Getting Help

If you get stuck:
  1. Check the code examples carefully - they include all necessary imports
  2. Review the notes and warnings in each section
  3. Consult the SQLAlchemy documentation for detailed API references
  4. Join the SQLAlchemy community on GitHub Discussions

Next Steps

Ready to begin? Start with the Unified Tutorial to learn the fundamentals of SQLAlchemy Core and ORM together.

Start Learning

Begin with the SQLAlchemy Unified Tutorial

Build docs developers (and LLMs) love