# Parameterize your SQL query

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.

<figure><img src="/files/TfXOwCqJNAp3Oz4U6ACj" alt=""><figcaption><p>Example of parameterizing SQL queries</p></figcaption></figure>

### Using Python

DataLab uses [Jinja](https://jinja.palletsprojects.com/en/3.0.x/intro/) to process your SQL queries and thus supports all Jinja syntax.&#x20;

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

<pre class="language-sql" data-line-numbers><code class="lang-sql"><strong>SELECT *
</strong>FROM dvdrentals.category C
WHERE C.category_id = {{ id }}
</code></pre>

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

{% code lineNumbers="true" %}

```sql
SELECT *
FROM dvdrentals.category C
WHERE C.category_id IN {{ ids | inclause }}
```

{% endcode %}

We use [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) 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:

{% code lineNumbers="true" %}

```sql
SELECT *
FROM dvdrentals.category
WHERE {{column | sqlsafe}} = 10
```

{% endcode %}

### Using R

DataLab uses [jinjar](https://github.com/davidchall/jinjar) to process your SQL queries and thus supports most Jinja syntax in R.&#x20;

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

<pre class="language-sql" data-line-numbers><code class="lang-sql"><strong>SELECT *
</strong>FROM dvdrentals.category C
WHERE C.category_id = {{ id }}
</code></pre>

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

```sql
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=",")
```

{% code lineNumbers="true" %}

```sql
SELECT *
FROM dvdrentals.category C
WHERE C.category_id IN ( {{ ids_for_sql }} )
```

{% endcode %}

Unlike for Python, DataLab can't protect against [SQL malicious code injection](https://en.wikipedia.org/wiki/SQL_injection) in interpolated SQL queries, so parametrize your queries with care.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://datalab-docs.datacamp.com/work/sql-cell/parameterize-your-sql-query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
