---
layout: docu
title: marimo Notebooks
---
[marimo](https://github.com/marimo-team/marimo) is an open-source reactive
notebook for Python and SQL that's tightly integrated with DuckDB's Python
client, letting you mix and match Python and SQL in a single git-versionable
notebook. Unlike traditional notebooks, when you run a cell or interact with a
UI element, marimo automatically (or lazily) runs affected cells, keeping code
and outputs consistent. Its integration with DuckDB makes it well-suited to
interactively working with data, and its representation as a Python file makes
it simple to run notebooks as scripts.
## Installation
To get started, install marimo and DuckDB from your terminal:
```bash
pip install "marimo[sql]" # or uv add "marimo[sql]"
```
Install supporting libraries:
```bash
pip install "polars[pyarrow]" # or uv add "polars[pyarrow]"
```
Run a tutorial:
```bash
marimo tutorial sql
```
## SQL in marimo
Create a notebook from your terminal with `marimo edit notebook.py`. Create SQL
cells in one of three ways:
1. Right-click the **+** button and pick **SQL cell**
2. Convert any empty cell to SQL via the cell menu
3. Hit the SQL button at the bottom of your notebook
In marimo, SQL cells give the appearance of writing SQL while being serialized as standard Python code using the `mo.sql()` function, which keeps your notebook as pure Python code without requiring special syntax or magic commands.
```python
df = mo.sql(f"SELECT 'Off and flying!' AS a_duckdb_column")
```
This is because marimo stores notebooks as pure Python, [for many reasons](https://marimo.io/blog/python-not-json), such as git-friendly diffs and running notebooks as Python scripts.
The SQL statement itself is an f-string, letting you interpolate Python values into the query with `{}` (shown later). In particular, this means your SQL queries can depend on the values of UI elements or other Python values, all part of marimo's dataflow graph.
> Warning Heads up!
> If you have user-generated content going into the SQL queries, be sure to sanitize your inputs to prevent SQL injection.
## Connecting a Custom DuckDB Connection
To connect to a custom DuckDB connection instead of using the default global connection, create a cell and create a DuckDB connection as Python variable:
```python
import duckdb
# Create a DuckDB connection
conn = duckdb.connect("path/to/my/duckdb.db")
```
marimo automatically discovers the connection and lets you select it in the SQL cell's connection dropdown.