---
title: "SQLAlchemy"
description: "SQLAlchemy is a popular Python library that provides a flexible way to interact with databases without writing raw SQL code."
canonical: "https://motherduck.com/glossary/sqlalchemy/"
related:
  - title: "SQLAlchemy with DuckDB and MotherDuck | MotherDuck Docs"
    url: "https://motherduck.com/docs/integrations/language-apis-and-drivers/python/sqlalchemy/"
  - title: "DuckLake: The Definitive Guide — Live Author Q&A with Matt Martin & Alex Monahan | MotherDuck"
    url: "https://motherduck.com/videos/ducklake-definitive-guide-oreilly-book/"
  - title: "Why Python Developers Need DuckDB (And Not Just Another DataFrame Library)"
    url: "https://motherduck.com/blog/python-duckdb-vs-dataframe-libraries/"
gated_asset:
  title: "DuckLake: The Lakehouse Table Format"
  url: "https://motherduck.com/lp/ducklake-lakehouse-table-format-book-full/"
---

# SQLAlchemy

> SQLAlchemy is a popular Python library that provides a flexible way to interact with databases without writing raw SQL code.

## Overview

SQLAlchemy is a popular Python library that provides a flexible way to interact with databases without writing raw SQL code. Created by Mike Bayer, [SQLAlchemy](https://www.sqlalchemy.org/) serves as a bridge between Python applications and databases, offering both high-level object-relational mapping (ORM) capabilities and low-level database access.

<glossary-callout guide="duckdb-book-brief" />

## Core Components

The library consists of two main components: SQLAlchemy Core and SQLAlchemy ORM. The Core component provides a SQL abstraction toolkit that allows you to construct database queries using Python methods and objects. The ORM (Object Relational Mapper) lets you interact with your database using Python classes and objects, abstracting away the underlying database operations entirely.

## Usage with DuckDB

DuckDB supports SQLAlchemy through the `duckdb_engine` package. After installing both SQLAlchemy and the DuckDB engine, you can create a connection using:

```python
from sqlalchemy import create_engine
engine = create_engine('duckdb:///path/to/database.db')
```

For in-memory DuckDB databases, you can use:

```python
engine = create_engine('duckdb://:memory:')
```

## Benefits for Data Work

For data analysts and engineers, SQLAlchemy provides several advantages. It handles database connections and transaction management automatically, supports multiple database backends with minimal code changes, and helps prevent SQL injection attacks. The library also integrates well with popular data science tools like pandas, allowing seamless data transfer between DataFrames and databases.

## Common Patterns

Instead of writing raw SQL like:
```sql
SELECT * FROM users WHERE age > 21
```

You can write SQLAlchemy code like:
```python
from sqlalchemy import select
query = select(users).where(users.c.age > 21)
```

This approach provides type safety, better code organization, and easier maintenance while still maintaining the full power of SQL operations.
