The information you want to present in any application is typically spread over numerous tables in a well-designed database. A query can collect data from different tables/ documents and pull it together for display. You can either request data from your database or manipulate the data, or both.
To write and execute queries, you should understand the basics of connecting to a Database or Connecting to an API.
A query can insert, update, or remove data from a database, conduct calculations, integrate data from other databases, and much more.
APIs, Database Queries, and Integrations to perform CRUD operations on data are collectively termed Queries in Appsmith.
You can create queries to manipulate data from a datasource using the Appsmith query editor.
By default, the prepared statement is enabled for all queries. Prepared statements provide a secure way of executing your queries. You can read How to Use Prepared Statements for efficient and secure data transactions.

Setting up a Query

Go to the page where you wish to run the query. Follow the below steps to set up a query using a query editor:
Queries are auto-saved, so you never lose your work. These changes will be reflected in your published application only after you deploy.
How to set up a Query?
  • Go to Datasources → + to create a new query or select an existing datasource to which you wish to add a query.
You can follow the steps detailed in Datasources to add a new datasource.
  • Choose the connected datasource and click + New Query. You will be taken to the query editor to configure the query. You can also choose to add a new query by navigating to Explorer >> Scroll down to Queries/JS >> Click plus (+) sign >> Select the name <DATASOURCE_NAME> Query (users query)
Create a Query from QUERIES/JS Menu Item
You can configure your API(s) using a REST interface.
The query is created with a default name. It’s recommended that you rename it for readability and access.
  • Write your query in the syntax that is valid for your database type.
  • Bind your query to the widget and carry out data manipulations.
A query and its results can be accessed from only the page it is a part of. To use a query on another page, click the context menu next to the query name and clone it.

Naming a Query

A query must have a unique and meaningful name that acts as an identifier. It is used to access the query results. In that sense, a name is like a variable in a programming language. You can access the various properties of the query and its data using the query name.
Note that JavaScript keywords and the window object methods and properties are not valid as query names.

Running a Query

Click on the Run button or hit cmd + enter to execute a query. You'll see the results in the Response tab if the query succeeds.
How to run a query?
All query results are stored in the data property of the query object. It is immutable and cannot be changed. You can access it using JavaScript, as shown in the code snippet below:
{{ Query1.data }}
DB Queries fetch data in the form of an array of objects where each object is a row, and each key in the object is a column.
All API Queries return the exact API response of the endpoint.

Using Queries in applications

Once you have successfully run a Query, you can use it in your application to
Copy link
Edit on GitHub
On this page
Setting up a Query
Naming a Query
Running a Query
Using Queries in applications