Skip to main content

SQL Queries

marimo provides first-class support for SQL through the mo.sql() function. Query dataframes, databases, and data warehouses directly in your notebooks with automatic result visualization.
SQL cells in marimo are reactive. When data changes, dependent SQL queries automatically re-run.

Quick Start

The simplest way to use SQL in marimo is with the default DuckDB engine:
The result is automatically displayed as an interactive table and returned as a dataframe.

Default Engine: DuckDB

By default, mo.sql() uses DuckDB to execute queries. DuckDB is a fast, in-memory analytical database that’s perfect for data analysis.
DuckDB is:
  • Fast: Optimized for analytical queries
  • Convenient: Runs in-process, no server needed
  • Flexible: Can query CSV, Parquet, JSON files directly
  • Compatible: Works with pandas and polars dataframes

Querying DataFrames

Any dataframes in your notebook’s global namespace can be queried directly:
Reference dataframe variables by name in your SQL queries. DuckDB automatically recognizes them!

Querying Files

DuckDB can read from various file formats directly:

Creating Tables

Create persistent tables within your notebook session:

Working with Results

SQL query results are returned as dataframes that you can use in Python:

Custom Database Engines

You can use mo.sql() with custom database connections for PostgreSQL, SQLite, MySQL, and more.

SQLAlchemy Connections

Connect to any database supported by SQLAlchemy:

DB-API 2.0 Connections

Use any DB-API 2.0 compatible connection:

ADBC Drivers

marimo supports ADBC (Arrow Database Connectivity) drivers for high-performance database access:

Supported Engines

marimo supports a wide variety of database engines:

DuckDB

Default engine, perfect for analytics

PostgreSQL

Via SQLAlchemy or ADBC

SQLite

Via SQLAlchemy or sqlite3

MySQL

Via SQLAlchemy

ClickHouse

High-performance analytics

Redshift

AWS data warehouse

Ibis

Portable Python dataframe API

ADBC Drivers

Arrow-native database connectivity

Output Control

Control whether query results are displayed:

Result Limits

Control the number of rows displayed using environment variables:
When no LIMIT is specified and MARIMO_SQL_DEFAULT_LIMIT is set, marimo automatically applies the limit to prevent loading too much data into memory.

EXPLAIN Queries

Use EXPLAIN to understand query execution:
DuckDB EXPLAIN output is displayed in a formatted, readable way.

Advanced Examples

Joining Multiple DataFrames

Window Functions

Common Table Expressions (CTEs)

Reading from URLs

Best Practices

1

Use descriptive variable names

Name your query results clearly to make your notebook readable.
2

Add LIMIT clauses

For large datasets, always include LIMIT to prevent loading too much data.
3

Leverage DuckDB's power

DuckDB can read many file formats directly - use this to avoid unnecessary data loading.
4

Use CTEs for complex queries

Break complex queries into Common Table Expressions for readability.
SQL queries in marimo are reactive - when input dataframes change, dependent queries automatically re-run!