DataLab Docs
  • What is DataLab?
  • Work
    • Creating a workbook
    • Sharing a workbook
    • Managing a workbook
    • Code cell
      • Working with packages
    • Text cell
      • Including images
    • SQL cell
      • SQL scenarios
      • Parameterize your SQL query
    • Explore Data cell
    • Chart cell
      • Configuring your chart
      • Pivot charts
      • Migration guide
    • AI Assistant
    • Version history
    • Scheduled runs
    • Hiding and showing cells
    • Long-running cells
    • Report view
    • Environment variables
    • Git and GitHub
  • Connect to Data
    • Connect your data to DataLab
    • Sharing a Data Source
    • Airtable
    • Amazon Athena
    • Amazon S3
    • Databricks
    • Dropbox
    • Files
    • Google Drive
    • Google BigQuery
    • Google Sheets
    • MariaDB
    • Microsoft SQL Server
    • MongoDB
    • MotherDuck
    • MySQL
    • Oracle Database
    • PostgreSQL
    • Redshift
    • Snowflake
    • Supabase
  • Guides
    • Publish a notebook
    • Importing data from flat files
    • Resizing plots
    • Show Bokeh and Pyvis plots
  • Resources
    • Pricing
    • Manage group settings
    • Reporting for Group Admins
    • DataLab for education
    • Technical requirements
    • Addressing slow code
    • Address R vulnerability
    • Get help
Powered by GitBook
On this page

Was this helpful?

  1. Work
  2. SQL cell

SQL scenarios

PreviousSQL cellNextParameterize your SQL query

Last updated 1 month ago

Was this helpful?

The different types of data sources to query and the different modes of querying enable powerful workflows that are useful in different combinations. Learn more about different ways of using the SQL cell in SQL scenarios.

Query a database with SQL and return a data frame, continue in Python

Create a SQL cell to query a database with a SQL cell in DataFrame mode. Next, create a Python cell and write Python code to analyze the query, e.g. to create a visualization.

This approach works if you prefer Python over SQL and the SQL query you're executing doesn't return a huge result (there is a query limit of 100 MB for free users and 500 MB for Premium users).

Query a database with SQL and return a data frame, continue in SQL

Create a SQL cell to query a database with a SQL cell in DataFrame mode. Next, create another SQL cell, select "DataFrames and CSVs" as a source and write SQL code to query the data frame created by the first SQL cell.

Query a CSV file or Excel file with SQL

Use this approach if you prefer SQL over Python to analyze tabular data but have the data in a CSV file or any other supported file type by DuckDB.

Query a database with SQL and reference it in another SQL cell

You typically do this if you want to more easily debug a complex SQL query that consists of one or more common table expressions.

Rather than writing this:

You can write this:

This approach works if your first query would return a huge result. Only a preview of that query result will be shown. When you execute the second query, the 'CTE-based query' is recompiled and executed on the database side rather than locally inside your workbook. You can review the compiled SQL that's being executed by by clicking on the "View Compiled SQL" icon:

This approach works if you prefer SQL over Python and the first SQL query you're executing doesn't return a huge result (there is a query limit of 100 MB for free users and 500 MB for paying users). The second SQL query will execute right inside DataLab using . The SQL syntax is similar to PostgreSQL; check the for a full overview.

Make sure the CSV file or Excel file you want to query is in your workbook file system by uploading it through the file browser. Next, create a SQL cell, select "DataFrames and CSVs" in the first dropdown and write SQL code to query the data file. The SQL query will execute right inside DataLab using . The SQL syntax is similar to PostgreSQL; check the (, ) for a full overview. Parquet and JSON files are also supported ().

DuckDB
DuckDB docs
DuckDB
DuckDB docs
CSV
Excel
Docs
Query a database with SQL, continue in Python
Query a database with SQL, continue analyzing the dataframe with SQL
Query a CSV file with SQL
Query an Excel file with SQL
SQL Query with a common table expression
CTE turned into a SQL cell in "Query mode" that is referenced in a second SQL cell.
You can review the compiled SQL code that gets executed on the database side.