Skip to main content

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:

  1. Create and configure a query.
  2. Save the query.
  3. Share the query using the Share button. This makes your query visible and editable to every Editor in your team. You can also import the query in your apps.
note

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.

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

tip

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

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 and select Import from Query Library from the Resource dropdown.

note

Every time you save a query in the library Retool creates 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

You can define variables by wrapping their names in {{ }} curly braces within reusable queries. For example, if you write select * from users where age > {{ min_age }} and age < {{ max_age }}, Retool detects two variables, min_age and max_age.

Within an app, you can pass component values into variables. This allows users to pass dynamic values into queries. For example, you can set the variable on a query to the value of a Text Input component.

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 a numerical input, and Retool evaluates it as retired or working based on the value.