Share queries

Learn how to reuse queries with the Query Library.

You can use the same query across multiple apps with Reusable Queries. Often, you want the same functionality across apps, but donโ€™t want to copy paste the same query over and over again. Reusable queries help you avoid code duplication, version control queries, and share queries with your team.

Write your query

Reusable queries are written in the Query Library. To set up a reusable query, you would:

  1. Click New to create a new query. Select a resource, write a query of your choosing, and Save it.
  2. Share your query using the Share button. This makes your query visible and editable to every Editor in your team. You can also now import this query in your apps.

You can define Variables for this query by using the curly braces {{ }}. Variables are dynamic inputs to your query. You can define defaults in the Variable section of the right hand panel. When you import a query in a Retool app you can pass inputs to the variables you define here.


Tip: You can write JavaScript inside the {{ }} curly braces

This works similarly to a regular Retool app. In this case, we parse words inside the braces as variables and display them in the Variables section on the right. You can try values here and test running the query. For example โ€” {{ { my_start_date: moment() } }}

Import your query

Now that your query has been shared, you can import it inside any Retool app. To do this, create a new query in your app, click on the Resources dropdown, and select "Import from Query Library".


Note: Each library query has a history

Every time you save a query in the library we create a new version. By default, when you import a shared query into an app, it will be in sync with the latest version. You also have the option to choose a specific version instead.

Using variables

Any words inside {{ }} curly braces in a reusable query will be parsed as variables. For example, if you write select * from users where age > {{ min_age }} and age < {{ max_age }}, Retool will detect two variables, min_age and max_age.

You can also write more complicated JavaScript expressions. Consider this query: select * from users where status = {{ age > 65 ? 'retired': 'working' }}. In this example, you can pass in age as an numerical input, and weโ€™ll evaluate it to โ€˜retiredโ€™ or โ€˜workingโ€™ based on the value.

Figure 3. An imported query passing the value from textinput1 to the store_id parameter


What permissions do I need to see / edit / use library queries?

By default all users have access to the Query Library. If you're an admin, you can configure which user groups have access to the Query library in your Permission settings.