ByteRun Docs
SQL and data

SQL cells

Two ways to run SQL in a notebook.

Native SQL cells

A native SQL cell contains only SQL. It runs on your data connection through ByteRun's Data Gateway, not on the notebook's kernel, so it works even while the project's compute is stopped.

Native cells run on PostgreSQL, MySQL, Snowflake and BigQuery connections. Amazon S3 holds files rather than a SQL engine, so S3 data is queried from %%sql or Python cells instead. Snowflake needs a connection that uses a key pair; a password-based Snowflake connection is refused when the cell runs, with a message saying so.

Queries are read-only. A result shows up to 1,000 rows by default; the cell's limit can raise that to at most 10,000. Under the result, Studio offers a line that loads it into Python: df = byterun.load_result("<id>"). A result can be loaded for about 10 minutes after the cell ran.

The cell's settings are stored in the notebook file as cell metadata:

{
  "byterun": {
    "cell_type": "sql",
    "execution": "native",
    "connection_name": "warehouse",
    "output_variable": "orders_df",
    "limit": 1000
  }
}

connection_name picks the data connection. output_variable and limit are optional. Opened in plain JupyterLab, a native SQL cell is simply a code cell.

%%sql cells

A %%sql cell is a Python cell whose first line is the jupysql magic, optionally naming a connection:

%%sql warehouse
SELECT * FROM orders LIMIT 10

It runs on the notebook's kernel, so the project's compute must be running. Every connection you can use is registered as an alias, so the name after %%sql is the connection's name. The Studio shows these cells with SQL highlighting.

Which to use

Use native cells to explore data cheaply, because they need no running compute. Use %%sql when you want the result inside the kernel's Python session next to the rest of your code.

A notebook with native SQL cells can be saved as a plain .py file that runs outside ByteRun; see export to Python.

Table results are shown as a table in both cases. In the playground, SQL runs against CSV files in your browser instead.

A SQL cell in the playground with its result table, run on in-browser SQLite

On this page