---
title: "Table variables"
description: "Table variables let you dynamically select which database table to query in the SQL editor."
url: "https://www.metabase.com/docs/latest/questions/native-editor/table-variables"
version: "65"
---

> For the complete documentation index, see [llms.txt](https://www.metabase.com/docs/llms.txt).

# Table variables

![Table variables](https://www.metabase.com/docs/latest/questions/images/table-variable.png)

Table variables let you write a SQL query with a placeholder for the table name. When you run the query, Metabase substitutes the variable with the schema and table name.

Table variables are especially useful when combined with [snippets](https://www.metabase.com/docs/latest/questions/native-editor/snippets.md), so you can write a generic query once and reuse it across different tables.

## Adding a table variable to a query

Use double braces where you'd normally write a table name:

```sql

SELECT
  COUNT(*)
FROM
  {{table}}

```

You can use table variables anywhere a table name would go, including `FROM` and `JOIN` clauses:

```sql

SELECT
  t.*,
  p.title
FROM
  {{table}} AS t
  JOIN {{products_table}} AS p ON t.product_id = p.id

```

See [Referencing table variables](#reference-table-variables).

### Setting the variable type to Table

After adding a `{{variable}}` to your query:

1. Open the **Variables** sidebar (it should appear automatically).
2. Change the variable type to **Table**.
3. Under **Table to map to**, select a table from the picker (Required).
4. Depending on how you want to refer to the table variable in the query, toggle **Emit table alias** on or off, see [Referencing table variables](#reference-table-variables).

When you run the query, Metabase replaces the variable with the selected table's schema and table name. To preview the code Metabase will run, click the **eye** icon.

## Reference table variables

There are two ways you can refer to the table variable in the rest of the query:

1. If you want to use the variable's name, you'll need to toggle _on_ **Emit table alias** in the variable's settings. Your queries will look like this:

   ```sql
   
   SELECT
       var_name.id,
       p.title
   FROM
       {{var_name}} JOIN products as p on var_name.product_id = p.id

   
   ```

2. If you want to specify an alias, you must toggle **Emit table alias** _off_ and manually add aliases to your query. You may want to specify your own alias if you already have a long query with existing aliases, and you just want to swap the table for a table variable. Your queries will look like this:

   ```sql
   
   SELECT
       o.id,
       p.title
   FROM
       {{var_name}} as o JOIN products as p on o.product_id = p.id

   
   ```

## Using table variables with snippets

One neat thing you can do is combine table variables with [snippets](https://www.metabase.com/docs/latest/questions/native-editor/snippets.md). You can write a generic query as a snippet and reuse the snippet in multiple questions, each mapped to a different table.

For example, create a snippet called "row count" with:

```sql

SELECT
  COUNT(*)
FROM
  {{table}}

```

Then insert the snippet into different questions:

```sql

{{snippet: row count}}

```

In each question, open the Variables sidebar and map `{{table}}` to a different database table. This way, the same snippet can count rows in `Products`, `Orders`, or any other table.

## Limitations

- **Not available as dashboard filter parameters.** You can't connect a table variable to a dashboard filter widget. Table variables must be set directly on each question.
- **SQL queries only.** Table variables are available in native SQL queries, not in the query builder.
- **No input widget.** There's no input widget for people to plug in a table. You must select the table to insert into the variable from the variables sidebar.
- **Not supported in transforms.** Table variables aren't available in [transforms](https://www.metabase.com/docs/latest/data-modeling/transforms/transforms-overview.md) yet.

## Further reading

- [SQL parameters](https://www.metabase.com/docs/latest/questions/native-editor/sql-parameters.md)
- [Snippets](https://www.metabase.com/docs/latest/questions/native-editor/snippets.md)
- [SQL troubleshooting guide](https://www.metabase.com/docs/latest/troubleshooting-guide/sql.md)
