Write data to SQL databases
Learn how to construct queries to safely write data to SQL databases and similar data stores.
You can create SQL queries using a visual interface (GUI mode) to safely write data to a connected database resource, such as a PostgreSQL or a MySQL database. This method helps prevent against destructive actions that malformed SQL statements could cause, such as dropping a table or overwriting existing data with incorrect values.
Clicking Preview on a query in SQL mode runs the query. For example, previewing a query in SQL mode that drops a table will in reality drop the table. Retool strongly recommends using GUI mode only when performing potentially destructive actions. Refer to Previewing queries for more information.
- Web or mobile app
- Workflow
1. Add a query
- Web or mobile app
- Workflow
- Navigate to the Code tab in the IDE.
- Click + to add a new query.
- Select the resource.

- Either:
- Right-click on the canvas and select the Resource query block.
- Click ⊕ in the left toolbar to open the Blocks tab, then select Resource query block.
- Click-and-drag from an existing block to create a new, connected block.
- Select the resource.

There are two modes from which to select when writing an SQL query. The mode you choose depends on whether you need to read or write data:
- SQL: Read data using an SQL statement.
- GUI: Write data using a graphical query editor.
Select GUI mode.