<p>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.</p>

<h2><a href="/docs/queries#query-compatibility" id="queryable-compatibility">Query Compatibility</a></h2>
<p>Each list endpoint supports different query operations. You can find the supported query methods and attributes in the <b>References</b> section of the Appwrite documentation.</p>

<h2><a href="/docs/queries#query-class" id="query-class">Query Class</a></h2>
<p>Appwrite SDKs provide a <code>Query</code> class to help you build queries. The <code>Query</code> class has a method for each type of supported query.</p>
<table cellspacing="0" cellpadding="0" border="0" class="full margin-bottom-large">
    <thead>
        <tr>
            <th style="width: 100px">Query Method</th>
            <th>SDK Method Example</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Select</td>
            <td><code>Query.select(["name", "title"])</code></td>
            <td>Select which attributes should be returned from a document.</td>
        </tr>
        <tr>
            <td>Equal</td>
            <td><code>Query.equal("title", ["Iron Man"])</code></td>
            <td>Returns document if attribute is equal to any value in the provided array.</td>
        </tr>
        <tr>
            <td>Not Equal</td>
            <td><code>Query.notEqual("title", ["Iron Man"])</code></td>
            <td>Returns document if attribute is not equal to any value in the provided array.</td>
        </tr>
        <tr>
            <td>Less Than</td>
            <td><code>Query.lessThan("score", 10)</code></td>
            <td>Returns document if attribute is less than the provided value.</td>
        </tr>
        <tr>
            <td>Less Than or Equal</td>
            <td><code>Query.lessThanEqual("score", 10)</code></td>
            <td>Returns document if attribute is less than or equal to the provided value.</td>
        </tr>
        <tr>
            <td>Greater Than</td>
            <td><code>Query.greaterThan("score", 10)</code></td>
            <td>Returns document if attribute is greater than the provided value.</td>
        </tr>
        <tr>
            <td>Greater Than or Equal</td>
            <td><code>Query.greaterThanEqual("score", 10)</code></td>
            <td>Returns document if attribute is greater than or equal to the provided value.</td>
        </tr>
        <tr>
            <td>Between</td>
            <td><code>Query.between("price", 5, 10)</code></td>
            <td>Returns document if attribute value falls between the two values. The boundary values are inclusive and can be strings or numbers.</td>
        </tr>
        <tr>
            <td>Is Null</td>
            <td><code>Query.isNull("name")</code></td>
            <td>Returns documents where attribute value is null.</td>
        </tr>
        <tr>
            <td>Is Not Null</td>
            <td><code>Query.isNotNull("name")</code></td>
            <td>Returns documents where attribute value is <b>not</b> null.</td>
        </tr>
        <tr>
            <td>Starts With</td>
            <td><code>Query.startsWith("name", "Once upon a time")</code></td>
            <td>Returns documents if a string attributes starts with a substring.</td>
        </tr>
        <tr>
            <td>Ends With</td>
            <td><code>Query.endsWith("name", "happily ever after.")</code></td>
            <td>Returns documents if a string attributes ends with a substring.</td>
        </tr>
        <tr>
            <td>Search</td>
            <td><code>Query.search("text", "key words")</code></td>
            <td>Searches string attributes for provided keywords. Requires a <a href="/docs/databases#indexes">Full-text index</a> on queried attributes.</td>
        </tr>
        <tr>
            <td>Order Descending</td>
            <td><code>Query.orderDesc("attribute")</code></td>
            <td>Orders results in descending order by attribute. Attribute must be indexed. Pass in an empty string to return in natural order.</td>
        </tr>
        <tr>
            <td>Order Ascending</td>
            <td><code>Query.orderAsc("attribute")</code></td>
            <td>Orders results in ascending order by attribute. Attribute must be indexed. Pass in an empty string to return in natural order.</td>
        </tr>
        <tr>
            <td>Limit</td>
            <td><code>Query.limit(25)</code></td>
            <td>Limits the number of results returned by the query. Used for <a href="/docs/pagination#offset-pagination" id="offset-pagination">pagination</a>. If the limit query is not used, the limit defaults to 25 results.</td>
        </tr>
        <tr>
            <td>Offset</td>
            <td><code>Query.offset(0)</code></td>
            <td>Offset the results returned by skipping some of the results. Used for <a href="/docs/pagination#offset-pagination" id="offset-pagination">pagination</a>.</td>
        </tr>
        <tr>
            <td>Cursor After</td>
            <td><code>Query.cursorAfter("62a7...f620")</code></td>
            <td>Places the cursor after the specified resource ID. Used for <a href="/docs/pagination#cursor-pagination" id="cursor-pagination">pagination</a>.</td>
        </tr>
        <tr>
            <td>Cursor Before</td>
            <td><code>Query.cursorBefore("62a7...a600")</code></td>
            <td>Places the cursor before the specified resource ID. Used for <a href="/docs/pagination#cursor-pagination" id="cursor-pagination">pagination</a>.</td>
        </tr>
    </tbody>
</table>


<h2><a href="/docs/queries#building-queries" id="building-queries">Building Queries</a></h2>
<p>
    Queries are passed to an endpoint through the <code>queries</code> parameter as an array of query strings, which can be generated using the <code>Query</code> class.
</p>
<p>Each query method is logically separated via <code>AND</code> operations. For <code>OR</code> operation, pass multiple values into the query method separated by commas. For example <code>Query.equal('title', ['Avatar', 'Lord of the Rings'])</code> will fetch the movies "Avatar" or "Lord of the Rings".</p>

<ul class="phases clear" data-ui-phases>
    <li>
        <h3>Web</h3>
        <div class="ide" data-lang="javascript" data-lang-label="Web SDK">
            <pre class="line-numbers"><code class="prism language-javascript" data-prism>import { Client, Databases, Query } from "appwrite";

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

const databases = new Databases(client);

databases.listDocuments(
    '[DATABASE_ID]',
    '[COLLECTION_ID]',
    [
        Query.equal('title', ['Avatar', 'Lord of the Rings']),
        Query.greaterThan('year', 1999)
    ]
);</code></pre>
        </div>
    </li>
    <li>
        <h3>Flutter</h3>
        <div class="ide" data-lang="dart" data-lang-label="Flutter SDK">
            <pre class="line-numbers"><code class="prism language-dart" data-prism>import 'package:appwrite/appwrite.dart';

void main() async {
    final client = Client()
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject('[PROJECT_ID]');

    final databases = Databases(client);

    try {
        final documents = await databases.listDocuments(
            '[DATABASE_ID]',
            '[COLLECTION_ID]',
            [
                Query.equal('title', ['Avatar', 'Lord of the Rings']),
                Query.greaterThan('year', 1999)
            ]
        );
    } on AppwriteException catch(e) {
        print(e);
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Android</h3>
        <div class="ide" data-lang="kotlin" data-lang-label="Android SDK">
            <pre class="line-numbers"><code class="prism language-kotlin" data-prism>import io.appwrite.Client
import io.appwrite.Query
import io.appwrite.services.Databases

suspend fun main() {
    val client = Client(applicationContext)
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject('[PROJECT_ID]');

    val databases = Databases(client)

    try {
        val documents = databases.listDocuments(
            databaseId = "[DATABASE_ID]",
            collectionId = "[COLLECTION_ID]",
            queries = listOf(
                Query.equal("title", listOf("Avatar", "Lord of the Rings")),
                Query.greaterThan("year", 1999)
            )
        )
    } catch (e: AppwriteException) {
        Log.e("Appwrite", e.message)
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Apple</h3>
        <div class="ide" data-lang="swift" data-lang-label="Apple SDK">
            <pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
import AppwriteModels

func main() async throws {
    let client = Client()
        .setEndpoint("https://cloud.appwrite.io/v1")
        .setProject("[PROJECT_ID]")

    let databases = Databases(client)

    do {
        let documents = try await databases.listDocuments(
            databaseId: "[DATABASE_ID]",
            collectionId: "[COLLECTION_ID]",
            queries: [
                Query.equal("title", ["Avatar", "Lord of the Rings"]),
                Query.greaterThan("year", 1999)
            ]
        )
    } catch {
        print(error.localizedDescription)
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>GraphQL</h3>
        <div class="ide" data-lang="graphql" data-lang-label="GraphQL">
            <pre class="line-numbers"><code class="prism language-graphql" data-prism>query {
    databasesListDocuments(
        databaseId: "[DATABASE_ID]",
        collectionId: "[COLLECTION_ID]"
        queries: ["equal(\"title\", [\"Avatar\", \"Lord of the Rings\"])", "greaterThan(\"year\", 1999)"]
    ) {
        total
        documents {
            _id
            data
        }
    }
}</code></pre>
        </div>
    </li>
</ul>

<h3><a href="/docs/databases#ordering" id="ordering">Ordering Results</a></h3>
<p>When querying using the <a href="/docs/server/databases#databasesListDocuments">listDocuments</a> endpoint, you can specify the order of the documents returned using the <code>Query.orderAsc()</code> and <code>Query.orderDesc()</code> query methods.</p>

<ul class="phases clear" data-ui-phases>
    <li>
        <h3>Web</h3>
        <div class="ide" data-lang="javascript" data-lang-label="Web SDK">
            <pre class="line-numbers"><code class="prism language-javascript" data-prism>import { Client, Databases, Query } from "appwrite";

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

const databases = new Databases(client);

databases.listDocuments(
    '[DATABASE_ID]',
    '[COLLECTION_ID]',
    [
        Query.orderAsc('title'),
    ]
);</code></pre>
        </div>
    </li>
    <li>
        <h3>Flutter</h3>
        <div class="ide" data-lang="dart" data-lang-label="Flutter SDK">
            <pre class="line-numbers"><code class="prism language-dart" data-prism>import 'package:appwrite/appwrite.dart';

void main() async {
    final client = Client()
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject('[PROJECT_ID]');

    final databases = Databases(client);

    try {
        final documents = await databases.listDocuments(
            databaseId: '[DATABASE_ID]',
            collectionId: '[COLLECTION_ID]',
            queries: [
                Query.orderAsc('title')
            ]
        );
    } on AppwriteException catch(e) {
        print(e);
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Android</h3>
        <div class="ide" data-lang="kotlin" data-lang-label="Android SDK">
            <pre class="line-numbers"><code class="prism language-kotlin" data-prism>import io.appwrite.Client
import io.appwrite.Query
import io.appwrite.services.Databases

suspend fun main() {
    val client = Client(applicationContext)
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject('[PROJECT_ID]');

    val databases = Databases(client)

    try {
        val documents = databases.listDocuments(
            databaseId = "[DATABASE_ID]",
            collectionId = "[COLLECTION_ID]",
            queries = [
                Query.orderAsc("title")
            ]
        )
    } catch (e: AppwriteException) {
        Log.e("Appwrite", e.message)
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Apple</h3>
        <div class="ide" data-lang="swift" data-lang-label="Apple SDK">
            <pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
import AppwriteModels

func main() async throws {
    let client = Client()
        .setEndpoint('https://cloud.appwrite.io/v1')
        .setProject('[PROJECT_ID]');

    let databases = Databases(client)

    do {
        let documents = try await databases.listDocuments(
            databaseId: "[DATABASE_ID]",
            collectionId: "[COLLECTION_ID]",
            queries: [
                Query.orderAsc("title")
            ]
        )
    } catch {
        print(error.localizedDescription)
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>GraphQL</h3>
        <div class="ide" data-lang="graphql" data-lang-label="GraphQL">
            <pre class="line-numbers"><code class="prism language-graphql" data-prism>query {
    databasesListDocuments(
        databaseId: "[DATABASE_ID]",
        collectionId: "[COLLECTION_ID]"
        queries: ["orderAsc(\"title\")"]
    ) {
        total
        documents {
            _id
            data
        }
    }
}</code></pre>
        </div>
    </li>
</ul>

<p>To sort based on multiple attributes, simply provide multiple query methods.</p>

<ul class="phases clear" data-ui-phases>
    <li>
        <h3>Web</h3>
        <div class="ide" data-lang="javascript" data-lang-label="Web SDK">
            <pre class="line-numbers"><code class="prism language-javascript" data-prism>import { Client, Databases, Query } from "appwrite";

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

const databases = new Databases(client);

databases.listDocuments(
    '[DATABASE_ID]',
    '[COLLECTION_ID]',
    [
        Query.orderAsc('title'), // Order by title in ascending order
        Query.orderDesc('year'), // Order by year in descending order
    ]
);</code></pre>
        </div>
    </li>
    <li>
        <h3>Flutter</h3>
        <div class="ide" data-lang="dart" data-lang-label="Flutter SDK">
            <pre class="line-numbers"><code class="prism language-dart" data-prism>import 'package:appwrite/appwrite.dart';

void main() async {
    final client = Client()
        .setEndpoint("https://cloud.appwrite.io/v1")
        .setProject("[PROJECT_ID]");

    final databases = Databases(client);
    try {
        final documents = await databases.listDocuments(
            databaseId: '[DATABASE_ID]',
            collectionId: '[COLLECTION_ID]',
            queries: [
                Query.orderAsc('title'), // Order by title in ascending order
                Query.orderDesc('year')  // Order by year in descending order
            ]
        );
    } on AppwriteException catch(e) {
        print(e);
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Android</h3>
        <div class="ide" data-lang="kotlin" data-lang-label="Android SDK">
            <pre class="line-numbers"><code class="prism language-kotlin" data-prism>import io.appwrite.Client
import io.appwrite.Query
import io.appwrite.services.Databases

suspend fun main() {
    val client = Client(applicationContext)
        .setEndpoint("https://cloud.appwrite.io/v1")
        .setProject("[PROJECT_ID]")

    val databases = Databases(client)

    try {
        val documents = databases.listDocuments(
            databaseId = "[DATABASE_ID]",
            collectionId = "[COLLECTION_ID]",
            queries = [
                Query.orderAsc("title"), // Order by title in ascending order
                Query.orderDesc("year")  // Order by year in descending order
            ]
        )
    } catch (e: AppwriteException) {
        Log.e("Appwrite", e.message)
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Apple</h3>
        <div class="ide" data-lang="swift" data-lang-label="Apple SDK">
            <pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
import AppwriteModels

func main() async throws {
    let client = Client()
      .setEndpoint("https://cloud.appwrite.io/v1")
      .setProject("[PROJECT_ID]")

    let databases = Databases(client)

    do {
        let documents = try await databases.listDocuments(
            databaseId: "[DATABASE_ID]",
            collectionId: "[COLLECTION_ID]",
            queries: [
                Query.orderAsc("title"), // Order by title in ascending order
                Query.orderDesc("year")  // Order by year in descending order
            ]
        )
    } catch {
        print(error.localizedDescription)
    }
}</code></pre>
        </div>
    </li>
    <li>
        <h3>GraphQL</h3>
        <div class="ide" data-lang="graphql" data-lang-label="GraphQL">
            <pre class="line-numbers"><code class="prism language-graphql" data-prism>query {
    databasesListDocuments(
        databaseId: "[DATABASE_ID]",
        collectionId: "[COLLECTION_ID]"
        queries: ["orderAsc(\"title\")", "orderDesc(\"year\")"]
    ) {
        total
        documents {
            _id
            data
        }
    }
}</code></pre>
        </div>
    </li>
</ul>

<p>In the example above, the movies returned will be first sorted by <code>title</code> in ascending order, then sorted by <code>year</code> in descending order.</p>