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
  • Using Python
  • Using R

Was this helpful?

  1. Work
  2. SQL cell

Parameterize your SQL query

PreviousSQL scenariosNextExplore Data cell

Last updated 1 year ago

Was this helpful?

There are times you want to dynamically update your SQL queries based on results of previous calculations or other data in your notebook. DataLab supports this through SQL parameterization, which allows you to insert variables into your SQL queries. SQL parameterization is supported in both Python and R workbooks.

Using Python

To insert a simple variable into your query, wrap it in double curly braces ({{ }}):

SELECT *
FROM dvdrentals.category C
WHERE C.category_id = {{ id }}

To insert an array in a WHERE ... IN statement, you can use theinclause filter:

SELECT *
FROM dvdrentals.category C
WHERE C.category_id IN {{ ids | inclause }}
SELECT *
FROM dvdrentals.category
WHERE {{column | sqlsafe}} = 10

Using R

To insert a simple variable into your query, wrap it in double curly braces ({{ }}):

SELECT *
FROM dvdrentals.category C
WHERE C.category_id = {{ id }}

To insert an array in a WHERE ... IN statement, DataLab provides the join() function:

SELECT *
FROM dvdrentals.category C
WHERE C.category_id IN ( {{ join(ids, ",") }} )

Alternatively, you can choose to preprocess in R yourself and inject a string, like so:

ids_for_sql <- paste(ids, collapse=",")
SELECT *
FROM dvdrentals.category C
WHERE C.category_id IN ( {{ ids_for_sql }} )

DataLab uses to process your SQL queries and thus supports all Jinja syntax.

We use to ensure you cannot accidentally inject malicious code in your SQL query. If you want to escape this safety, use the sqlsafe filter to mark your parameter as safe:

DataLab uses to process your SQL queries and thus supports most Jinja syntax in R.

Unlike for Python, DataLab can't protect against in interpolated SQL queries, so parametrize your queries with care.

Jinja
prepared statements
jinjar
SQL malicious code injection
Example of parameterizing SQL queries