Mix SQL and Python in One Notebook
A free browser notebook that mixes DuckDB SQL and pandas cells. Query with SQL, continue in Python with df, and query DataFrames back with SQL.
Some steps are easiest in SQL (joins, group-bys, window functions) and others in pandas (reshaping, string cleanup, custom logic). The Notebook lets you switch between them cell by cell: a SQL result arrives in Python as df, and a DataFrame becomes a table that later SQL cells can query.
How it works
- Write a SQL cell to filter or join your data with DuckDB.
- In the next Python cell, use df — it holds the result of the SQL cell above.
- End a Python cell with a DataFrame and query it from a later SQL cell by its table name.
Examples you can copy
SQL cell
SELECT region, SUM(quantity * unit_price) AS revenue
FROM nb_orders
GROUP BY region;
Python cell using the SQL result
df["share"] = (df["revenue"] / df["revenue"].sum()).round(3)
df.sort_values("share", ascending=False)
FAQ
How does data move between SQL and Python?
When a Python cell uses df, the notebook reruns the nearest SQL cell above it and passes the result to pandas through Apache Arrow. A Python cell that ends with a DataFrame is registered as a DuckDB table for later SQL cells.
Is this like Jupyter with %%sql magic?
Similar idea, without setup: SQL cells are a built-in cell type backed by DuckDB, so there's no extension, connection string or database server.
Can I export to Jupyter?
Yes. Export the notebook as a .ipynb file to open it in Jupyter, or import an existing .ipynb.
Is there a size limit for the bridge?
Each transfer between SQL and Python is limited to 256 MB. Filter or aggregate in SQL first for larger data.
Open the Notebook
Explore the local data workbench · Browse all tools