[Skip to content](https://appwrite.io/docs/products/databases/tablesdb/queries#main-content)

Databases

Many list endpoints in Appwrite allow you to filter, sort, and paginate results using queries. Appwrite provides a common set of syntax to build queries.

## [Query class](https://appwrite.io/docs/products/databases/tablesdb/queries\#query-class)

Appwrite SDKs provide a `Query` class to help you build queries. The `Query` class has methods for each type of supported query operation.

## [Building queries](https://appwrite.io/docs/products/databases/tablesdb/queries\#building-queries)

Queries are passed to an endpoint through the `queries` parameter as an array of query strings, which can be generated using the `Query` class.

Each query method is logically separated via `AND` operations. For `OR` operation, pass multiple values into the query method separated by commas. For example `Query.equal('title', ['Avatar', 'Lord of the Rings'])` will fetch the movies `Avatar` or `Lord of the Rings`.

Default pagination behavior

By default, results are limited to the **first 25 items**. You can change this through [pagination](https://appwrite.io/docs/products/databases/pagination).

Web

Copy

```javascript
import { Client, Query, TablesDB } from "appwrite";

const client = new Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

const tablesDB = new TablesDB(client);

tablesDB.listRows({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    queries: [\
        Query.equal('title', ['Avatar', 'Lord of the Rings']),\
        Query.greaterThan('year', 1999)\
    ]
});
```

## [Query operators](https://appwrite.io/docs/products/databases/tablesdb/queries\#query-operators)

### [Select](https://appwrite.io/docs/products/databases/tablesdb/queries\#select)

The `select` operator allows you to specify which columns should be returned from a row. This is essential for optimizing response size, controlling which relationship data loads, and only retrieving the data you need.

Web

Copy

```javascript
Query.select(["name", "title"])
```

#### [Select relationship data](https://appwrite.io/docs/products/databases/tablesdb/queries\#relationship-select)

With [opt-in relationship loading](https://appwrite.io/docs/products/databases/relationships#performance-loading), you must explicitly select relationship data. This gives you fine-grained control over performance and payload size.

##### [Get rows without relationships](https://appwrite.io/docs/products/databases/tablesdb/queries\#get-rows-without-relationships)

By default, rows return only their own fields:

Web

Copy

```javascript
const row = await tablesDB.getRow({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    rowId: '<ROW_ID>',
    queries: [Query.select(['name', 'age'])]
});
```

##### [Load all relationship data](https://appwrite.io/docs/products/databases/tablesdb/queries\#load-all-relationship-data)

Use the `*` wildcard to load all fields from related rows:

Web

Copy

```javascript
const row = await tablesDB.getRow({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    rowId: '<ROW_ID>',
    queries: [Query.select(['*', 'reviews.*'])]
});
```

##### [Select specific relationship fields](https://appwrite.io/docs/products/databases/tablesdb/queries\#select-specific-relationship-fields)

For precise control, select only specific fields from related rows:

Web

Copy

```javascript
const row = await tablesDB.getRow({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    rowId: '<ROW_ID>',
    queries: [Query.select(['name', 'age', 'reviews.author', 'reviews.rating'])]
});
```

##### [Load nested relationships](https://appwrite.io/docs/products/databases/tablesdb/queries\#load-nested-relationships)

You can also load relationships of relationships:

Web

Copy

```javascript
Query.select(["*", "reviews.*", "reviews.author.*"])
```

#### [Use selection patterns](https://appwrite.io/docs/products/databases/tablesdb/queries\#select-patterns)

| Pattern | Description | Use case |
| --- | --- | --- |
| `["field1", "field2"]` | Specific columns only | Minimize response size |
| `["*"]` | All row columns | Get complete row data |
| `["*", "relationName.*"]` | Row + all relationship fields | Load row with complete related data |
| `["field1", "relationName.field2"]` | Specific fields from row and relationships | Precise data loading |
| `["*", "relationName.field1", "relationName.field2"]` | All row fields + specific relationship fields | Partial relationship loading |
| `["relationName.*", "relationName.nestedRelation.*"]` | Nested relationship loading | Load relationships of relationships |

#### [Optimize performance](https://appwrite.io/docs/products/databases/tablesdb/queries\#select-performance)

**Optimize response size** \- Only select the fields you actually need. Smaller responses are faster to transfer and parse.

**Control relationship loading** \- Related rows are not loaded by default. Use explicit selection to load only the relationships you need.

**Reduce database load** \- Selecting fewer fields reduces database processing time, especially for large rows.

Related rows

By default, relationship columns contain only row IDs. To load the actual related row data, you must explicitly include relationship fields in your select query. Learn more about [relationship performance optimization](https://appwrite.io/docs/products/databases/relationships#performance-loading).

### [Comparison operators](https://appwrite.io/docs/products/databases/tablesdb/queries\#comparison)

#### [Equal](https://appwrite.io/docs/products/databases/tablesdb/queries\#equal)

Returns row if column is equal to any value in the provided array. Also supported for spatial types.

Web

Copy

```javascript
Query.equal("title", ["Iron Man"])
```

#### [Not equal](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-equal)

Returns row if column is not equal to any value in the provided array. Also supported for spatial types.

Web

Copy

```javascript
Query.notEqual("title", "Iron Man")
```

#### [Less than](https://appwrite.io/docs/products/databases/tablesdb/queries\#less-than)

Returns row if column is less than the provided value.

Web

Copy

```javascript
Query.lessThan("score", 10)
```

#### [Less than or equal](https://appwrite.io/docs/products/databases/tablesdb/queries\#less-than-equal)

Returns row if column is less than or equal to the provided value.

Web

Copy

```javascript
Query.lessThanEqual("score", 10)
```

#### [Greater than](https://appwrite.io/docs/products/databases/tablesdb/queries\#greater-than)

Returns row if column is greater than the provided value.

Web

Copy

```javascript
Query.greaterThan("score", 10)
```

#### [Greater than or equal](https://appwrite.io/docs/products/databases/tablesdb/queries\#greater-than-equal)

Returns row if column is greater than or equal to the provided value.

Web

Copy

```javascript
Query.greaterThanEqual("score", 10)
```

#### [Between](https://appwrite.io/docs/products/databases/tablesdb/queries\#between)

Returns row if column value falls between the two values. The boundary values are inclusive and can be strings or numbers.

Web

Copy

```javascript
Query.between("price", 5, 10)
```

#### [Not between](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-between)

Returns rows if the column value is outside the range defined by the two values (strictly less than start OR strictly greater than end). Works with strings or numbers. Boundary values are excluded.

Web

Copy

```javascript
Query.notBetween("price", 5, 10)
```

### [Null checks](https://appwrite.io/docs/products/databases/tablesdb/queries\#null-checks)

#### [Is null](https://appwrite.io/docs/products/databases/tablesdb/queries\#is-null)

Returns rows where column value is null.

Web

Copy

```javascript
Query.isNull("name")
```

#### [Is not null](https://appwrite.io/docs/products/databases/tablesdb/queries\#is-not-null)

Returns rows where column value is **not** null.

Web

Copy

```javascript
Query.isNotNull("name")
```

### [Text operations](https://appwrite.io/docs/products/databases/tablesdb/queries\#text-operations)

#### [Starts with](https://appwrite.io/docs/products/databases/tablesdb/queries\#starts-with)

Returns rows if a text column starts with a substring.

Web

Copy

```javascript
Query.startsWith("name", "Once upon a time")
```

#### [Not starts with](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-starts-with)

Returns rows if a text column does not start with a substring.

Web

Copy

```javascript
Query.notStartsWith("name", "Once upon a time")
```

#### [Ends with](https://appwrite.io/docs/products/databases/tablesdb/queries\#ends-with)

Returns rows if a text column ends with a substring.

Web

Copy

```javascript
Query.endsWith("name", "happily ever after.")
```

#### [Not ends with](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-ends-with)

Returns rows if a text column does not end with a substring.

Web

Copy

```javascript
Query.notEndsWith("name", "happily ever after.")
```

#### [Contains](https://appwrite.io/docs/products/databases/tablesdb/queries\#contains)

Returns rows if the array column contains the specified elements or if a text column contains the specified substring. Also supported for spatial types.

Web

Copy

```javascript
// For arrays
Query.contains("ingredients", ['apple', 'banana'])

// For text columns
Query.contains("name", "Tom")
```

#### [Not contains](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-contains)

Returns rows if the array column does not contain the specified elements, or if a text column does not contain the specified substring. Also supported for spatial types.

Web

Copy

```javascript
// For arrays
Query.notContains("ingredients", ['apple', 'banana'])

// For text columns
Query.notContains("name", "Tom")
```

#### [Search](https://appwrite.io/docs/products/databases/tablesdb/queries\#search)

Searches text columns for provided keywords. Requires a [full-text index](https://appwrite.io/docs/products/databases/tables#indexes) on queried columns. The search string must be at least **3 characters** to perform a search.

Searching values with hyphens

The hyphen (`-`) is treated as a stop character by the underlying search engine. To search for exact values that contain hyphens (for example, ticket or SKU codes like `SWT-2621-44`), wrap the value in quotes inside the search string: `Query.search(column, '"SWT-2621-44"')`.

Web

Copy

```javascript
Query.search("text", "key words")
```

#### [Not search](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-search)

Returns rows if a text column does not match the full-text search query. Requires a [full-text index](https://appwrite.io/docs/products/databases/tables#indexes) on queried columns. The search string must be at least **3 characters** to perform a search.

Searching values with hyphens

The hyphen (`-`) is treated as a stop character by the underlying search engine. To exclude exact values that contain hyphens (for example, ticket or SKU codes like `SWT-2621-44`), wrap the value in quotes inside the search string: `Query.notSearch(column, '"SWT-2621-44"')`.

Web

Copy

```javascript
Query.notSearch("text", "key words")
```

### [Logical operators](https://appwrite.io/docs/products/databases/tablesdb/queries\#logical-operators)

#### [AND](https://appwrite.io/docs/products/databases/tablesdb/queries\#and)

Returns row if it matches all of the nested sub-queries in the array passed in.

Web

Copy

```javascript
Query.and([\
    Query.lessThan("size", 10),\
    Query.greaterThan("size", 5)\
])
```

#### [OR](https://appwrite.io/docs/products/databases/tablesdb/queries\#or)

Returns row if it matches any of the nested sub-queries in the array passed in.

Web

Copy

```javascript
Query.or([\
    Query.lessThan("size", 5),\
    Query.greaterThan("size", 10)\
])
```

### [Ordering](https://appwrite.io/docs/products/databases/tablesdb/queries\#ordering)

#### [Order descending](https://appwrite.io/docs/products/databases/tablesdb/queries\#order-desc)

Orders results in descending order by column. Column must be indexed.

Web

Copy

```javascript
Query.orderDesc("column")
```

#### [Order ascending](https://appwrite.io/docs/products/databases/tablesdb/queries\#order-asc)

Orders results in ascending order by column. Column must be indexed.

Web

Copy

```javascript
Query.orderAsc("column")
```

#### [Order random](https://appwrite.io/docs/products/databases/tablesdb/queries\#order-random)

Orders results in random order.

Web

Copy

```javascript
Query.orderRandom()
```

### [Pagination](https://appwrite.io/docs/products/databases/tablesdb/queries\#pagination)

#### [Limit](https://appwrite.io/docs/products/databases/tablesdb/queries\#limit)

Limits the number of results returned by the query. Used for [pagination](https://appwrite.io/docs/products/databases/pagination).

Web

Copy

```javascript
Query.limit(25)
```

#### [Offset](https://appwrite.io/docs/products/databases/tablesdb/queries\#offset)

Offset the results returned by skipping some of the results. Used for [pagination](https://appwrite.io/docs/products/databases/pagination).

Web

Copy

```javascript
Query.offset(0)
```

#### [Cursor after](https://appwrite.io/docs/products/databases/tablesdb/queries\#cursor-after)

Places the cursor after the specified resource ID. Used for [pagination](https://appwrite.io/docs/products/databases/pagination).

Web

Copy

```javascript
Query.cursorAfter("62a7...f620")
```

#### [Cursor before](https://appwrite.io/docs/products/databases/tablesdb/queries\#cursor-before)

Places the cursor before the specified resource ID. Used for [pagination](https://appwrite.io/docs/products/databases/pagination).

Web

Copy

```javascript
Query.cursorBefore("62a7...a600")
```

## [Time helpers](https://appwrite.io/docs/products/databases/tablesdb/queries\#time-helpers)

Built-in helpers for filtering by creation and update timestamps using ISO 8601 date-time strings (for example, "2025-01-01T00:00:00Z").

#### [Created before](https://appwrite.io/docs/products/databases/tablesdb/queries\#created-before)

Returns rows created before the given date.

Web

Copy

```javascript
Query.createdBefore("2025-01-01T00:00:00Z")
```

#### [Created after](https://appwrite.io/docs/products/databases/tablesdb/queries\#created-after)

Returns rows created after the given date.

Web

Copy

```javascript
Query.createdAfter("2025-01-01T00:00:00Z")
```

#### [Updated before](https://appwrite.io/docs/products/databases/tablesdb/queries\#updated-before)

Returns rows updated before the given date.

Web

Copy

```javascript
Query.updatedBefore("2025-01-01T00:00:00Z")
```

#### [Updated after](https://appwrite.io/docs/products/databases/tablesdb/queries\#updated-after)

Returns rows updated after the given date.

Web

Copy

```javascript
Query.updatedAfter("2025-01-01T00:00:00Z")
```

## [Geo queries and spatial operations](https://appwrite.io/docs/products/databases/tablesdb/queries\#geo-queries)

Geo queries enable geographic operations on [spatial columns](https://appwrite.io/docs/products/databases/spatial). Coordinates are specified as `[longitude, latitude]` arrays. Distance measurements can be specified in meters or degrees.

For conceptual information about spatial data types, spatial columns and indexing, see [Geo queries](https://appwrite.io/docs/products/databases/geo-queries).

Additional supported queries

In addition to the spatial-specific operations below, the query helpers `equal`, `notEqual`, `contains`, and `notContains` are also supported on spatial columns. This lets you match or exclude exact spatial values, check whether a geometry collection contains a geometry or not.

### [Distance equal](https://appwrite.io/docs/products/databases/tablesdb/queries\#distance-equal)

Returns rows where the spatial column is exactly the specified distance from a point.

Web

Copy

```javascript
// Coordinates: [longitude, latitude]
Query.distanceEqual("location", [-73.9851, 40.7589], 200)
```

### [Distance not equal](https://appwrite.io/docs/products/databases/tablesdb/queries\#distance-not-equal)

Returns rows where the spatial column is not exactly the specified distance from a point.

Web

Copy

```javascript
Query.distanceNotEqual("location", [-73.9851, 40.7589], 200)
```

### [Distance greater than](https://appwrite.io/docs/products/databases/tablesdb/queries\#distance-greater-than)

Returns rows where the spatial column is more than the specified distance from a point.

Web

Copy

```javascript
Query.distanceGreaterThan("location", [-73.9851, 40.7589], 200)
```

### [Distance less than](https://appwrite.io/docs/products/databases/tablesdb/queries\#distance-less-than)

Returns rows where the spatial column is less than the specified distance from a point.

Web

Copy

```javascript
Query.distanceLessThan("location", [-73.9851, 40.7589], 200)
```

### [Intersects](https://appwrite.io/docs/products/databases/tablesdb/queries\#intersects)

Returns rows where the spatial column intersects with the provided geometry.

Web

Copy

```javascript
Query.intersects("area", [[-73.9851, 40.7589], [-73.9776, 40.7614], [-73.9733, 40.7505], [-73.9851, 40.7589]])
```

### [Not intersects](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-intersects)

Returns rows where the spatial column does not intersect with the provided geometry.

Web

Copy

```javascript
Query.notIntersects("area", [[-73.9851, 40.7589], [-73.9776, 40.7614], [-73.9733, 40.7505], [-73.9851, 40.7589]])
```

### [Overlaps](https://appwrite.io/docs/products/databases/tablesdb/queries\#overlaps)

Returns rows where the spatial column overlaps with the provided geometry.

Web

Copy

```javascript
Query.overlaps("zone", [[-73.9851, 40.7589], [-73.9776, 40.7614], [-73.9733, 40.7505], [-73.9851, 40.7589]])
```

### [Not overlaps](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-overlaps)

Returns rows where the spatial column does not overlap with the provided geometry.

Web

Copy

```javascript
Query.notOverlaps("zone", [[-73.9851, 40.7589], [-73.9776, 40.7614], [-73.9733, 40.7505], [-73.9851, 40.7589]])
```

### [Touches](https://appwrite.io/docs/products/databases/tablesdb/queries\#touches)

Returns rows where the spatial column touches the provided geometry.

Web

Copy

```javascript
Query.touches("boundary", [[-73.9851, 40.7589], [-73.9776, 40.7614], [-73.9733, 40.7505], [-73.9851, 40.7589]])
```

### [Not touches](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-touches)

Returns rows where the spatial column does not touch the provided geometry.

Web

Copy

```javascript
Query.notTouches("boundary", [[-73.9851, 40.7589], [-73.9776, 40.7614], [-73.9733, 40.7505], [-73.9851, 40.7589]])
```

### [Crosses](https://appwrite.io/docs/products/databases/tablesdb/queries\#crosses)

Returns rows where the spatial column crosses the provided geometry.

Web

Copy

```javascript
Query.crosses("route", [[-73.9851, 40.7589], [-73.9776, 40.7614]])
```

### [Not crosses](https://appwrite.io/docs/products/databases/tablesdb/queries\#not-crosses)

Returns rows where the spatial column does not cross the provided geometry.

Web

Copy

```javascript
Query.notCrosses("route", [[-73.9851, 40.7589], [-73.9776, 40.7614]])
```

## [Complex queries](https://appwrite.io/docs/products/databases/tablesdb/queries\#complex-queries)

You can create complex queries by combining AND and OR operations. For example, to find items that are either books under $20 or magazines under $10:

Web

Copy

```javascript
const results = await tablesDB.listRows({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    queries: [\
        Query.or([\
            Query.and([\
                Query.equal('category', ['books']),\
                Query.lessThan('price', 20)\
            ]),\
            Query.and([\
                Query.equal('category', ['magazines']),\
                Query.lessThan('price', 10)\
            ])\
        ])\
    ]
});
```

This example demonstrates how to combine `OR` and `AND` operations. The query uses `Query.or()` to match either condition: books under $20 OR magazines under $10. Each condition within the OR is composed of two AND conditions - one for the category and one for the price threshold. The database will return rows that match either of these combined conditions.

Was this page helpful?

Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.

YesNo