Reading and writing data

Queries pull in data, so you can use them in components.

Queries let your components read or write data from various datasources. Retool supports querying most databases, as well as any API.

Your components then connect to queries via their props - for example, you could set your Table's data to be {{}}, and the table would then show data from query1. Similarly, you could change the onClick of a Button to trigger a {{resetPassword}} function for example, which could fire an API request.


Queries that don't mutate data (eg. read-only SQL statements, or GET API requests) are automatically run on page load. Then they populate their .data field, which you can access via Afterwards, when their parameters change (eg. select * from users where id = {{textinput1.value}}), read-only queries are automatically re-run.

Queries that mutate data are only run when you explicitly tell them to. For example, if you have a table of users, and you have a POST request that resets the selected user's password, the POST query won't automatically run when you change rows. Instead, POST and other write requests must be run explicitly, generally via a Button. That way, we won't write data back accidentally.

When do queries run and refresh?

Queries that read data (SQL statements + GET API queries) will automatically refresh when their parameters change. For example, the following query will auto-update every time textinput.value changes:

select * from users where customer_id = {{textinput.value}}


Queries that write data don't automatically run when their parameters change

Queries that write data (SQL actions with our GUI editor + all other HTTP methods excluding GET) will not automatically refresh when their parameters change. If you want them to change, you can select the "Run query automatically when inputs change" in the dropdown at the top of the query.

But you probably don't want that - since if your query pulls in the value from a TextInput, every time you entered a keystroke in the TextInput, the query would get called once. So you would find multiple rows inserted in your database: m, ma, mar, and mary. We suggest adding a button that triggers the POST.

Additional options

Triggering queries on success or failure

You'll often want to trigger more queries when a query finishes. For example, after you change something in a database table, you probably want to refresh the users query that pulls in all your users. So updateUsername should cause your users query to refresh.

To do that, add an event handler to run the users query after the updateUsername query runs successfully.

Refreshing users after updateEmail succeeds.Refreshing users after updateEmail succeeds.

Requiring confirmation before running

If you don't want a query to run automatically (whether because parameters change or because you triggered it), you can require confirmation before running. Before the query is run, a modal will pop up (the message can be dynamic), asking whether you're sure you want to run the query.

Requiring a confirmation message before running the delete user query.Requiring a confirmation message before running the delete user query.

Running queries periodically

Retool also provides the mechanism to periodically run a query in a user specified period. You can find this under the Advanced tab. Here is an example of this.

Example of a query refreshing every 5 seconds (5000 milliseconds)Example of a query refreshing every 5 seconds (5000 milliseconds)

Disabling queries dynamically

On more complex pages with many queries, it is sometimes useful to stop queries from running if they are not immediately needed. For example, if you are using a tabbed container, you might only want a query to run when the user switches the active tab to the second tab. To do this, you could write the following.

Only run a query when the second tab is active.Only run a query when the second tab is active.

Hiding parameters from audit logs

All run queries are logged in the audit logs. If you have a sensitive parameter (eg. an API key), you shouldn't log it. Just add the parameter to "drop from audit logs" dropdown.

Blacklisting the value of textinput1.value.Blacklisting the value of textinput1.value.

Query throttling

If you find that Retool performance is slow, it might be because your queries are refreshing too frequently (eg. on every keystroke). To solve that, you can throttle query such that it runs less frequently. The default is 750ms.

Watch inputs

If the query uses a dynamic input--for example, using a text input component to supply an ID parameter--and is triggered manually, you can set the query to watch and update in reaction to these inputs. In the Advanced tab under Advanced options, add any inputs to watch to the Watched inputs dropdown.

Timing options

You can control when queries run using options in the Timing section of the Advanced tab in the query editor.

Query timeout

Retool automatically times out queries that run for longer than 10 seconds (10000ms). You can adjust the Timeout after duration for queries on an individual basis in the Advanced tab of the query editor. For Retool-hosted users, the maximum duration before a query times out is 120 seconds (120000ms). Self-hosted users can set the DBCONNECTOR_QUERY_TIMEOUT_MS environment variable to change the maximum timeout value.

Delay between query runs

To update the amount of time to wait between query runs, in the Advanced tab, update the Delay between runs field. This field specifies the number of milliseconds to wait in between runs of the same query and defaults to 750 milliseconds.

Delay page load

If the query runs on page load, you can set a delay by updating the Page load delay (ms) field in the Advanced tab.

What’s Next

Here's how you read and write from specific resources: