# Creating Queries

In this section, we will create different queries to retrieve specific tasks from the database, namely :&#x20;

* [All Tasks](/overview/getting-started/creating-queries.md#all-tasks-query)
* [My Tasks](/overview/getting-started/creating-queries.md#my-tasks-query)
* [Tasks Due Today](/overview/getting-started/creating-queries.md#due-today-query)
* [Completed Tasks](/overview/getting-started/creating-queries.md#completed-tasks)

For in-depth documentation about Queries, you can check out the corresponding documentation page :

{% content-ref url="/pages/-M8HjWmJKLSXJ8bKprqX" %}
[Query Designer](/building-an-application/data/query-data.md)
{% endcontent-ref %}

### Creating a new **Query**

To create a new query, click on **Query** then click on **New Query**

![Creating a new query](/files/-MA7PPGDeka10-ew1gla)

### **All Tasks Query**

For this query, we simply want to retrieve all todos. We also give access to the **Viewer** group to run this query and access to the **Editor** group to modify this query.

In the **Common Tab** :&#x20;

* [ ] Set the **Source** to the To Do class
* [ ] Set **Run Credentials** to **Viewer**
* [ ] Set **Edit Credentials** to **Editor**
* [ ] Set the **Name** to `All Tasks`

In the **Fields Tab** we can select which fields we want the query to return. For this tutorial, we will retrieve all the fields. Check the **All** checkbox on the right and click on **Add Fields.** This will populate our query with all the available fields in our To Do class.

We also want to sort the results of this query by having the latest task created first, so we sort the **CreatedOn** field in descending order by clicking on the Order By Descending Button<img src="/files/-MA7VYJ3JtZNzAAkWr7Y" alt="" data-size="line">.

In the **Fields Tab** :&#x20;

* [ ] Add all fields
* [ ] Order the **CreatedOn** field by Descending Order

![All Tasks Query Properties](/files/-MAXjnTcpvnWaoIqyq_f)

When done, click the **Save** button to save the query.

### My Tasks Query

This query will only return tasks of which we are the owner and make use of **Conditions.**

In the **Common Tab** :&#x20;

* [ ] Set the **Source** to the To Do class
* [ ] Set **Run Credentials** to **Viewer**
* [ ] Set **Edit Credentials** to **Editor**
* [ ] Set the **Name** to `My Tasks`

In the **Fields Tab** :&#x20;

* [ ] Add all fields
* [ ] Order the **CreatedOn** field by Descending Order

![My Tasks Query Properties](/files/-MAXkqTJmCucsamVjsMp)

Now we want to compare our user ID to the Owner field of the To Do class to determine if it should be retrieved or not. To add a new condition to our query, switch to the **Conditions Tab,** and click **Add Condition.**

&#x20;On the right-hand side, choose the Owner field of your class. Click on the gear icon :gear:  to open the **Condition Properties** window. Set **Compare To** to **Expression** and **Value** to `{CurrentUser.ID}`

![My Task Query Condition Properties](/files/-MAXlYF4LMy7MYRymcnH)

### Due Today Query

We want this query to return all to incomplete task due today. This means that we will once again make use of conditions to check both the IsCompleted and the DueDate fields of our To Do class.

In the **Common Tab** :&#x20;

* [ ] Set the **Source** to the To Do class
* [ ] Set **Run Credentials** to **Viewer**
* [ ] Set **Edit Credentials** to **Editor**
* [ ] Set the **Name** to `Due Today`

In the **Fields Tab** :&#x20;

* [ ] Add all fields
* [ ] Order the **CreatedOn** field by Descending Order

![Due Today Query Properties](/files/-MABeL71hTMv2zR8qdAX)

In the **Conditions Tab**, we click on **Add Condition.** Our first condition we want to check is  `MyToDos.IsCompleted = 0`.

We now want to check if the due date is less or equal to today's date, which is denoted by `MyToDos.DueDate <= {DateTime.Now.Date}`.&#x20;

To add a second condition, click on **Add Condition** and make sure to choose **AND** as the first connector  Set the Field column to the  `MyToDos.DueDate` field of your class set the middle **Operator** to  **`<=`**.

Click on the gear icon :gear:to open the **Condition Properties** window. Set **Compare To** to **Expression** and **Value** to `{DateTime.Now.Date}`

![Due Today Condition Properties](/files/-MAXmPRNxeUFwIcjS5Yk)

### Completed Tasks

For this last query we simply want to return completed queries.

In the **Common Tab** :&#x20;

* [ ] Set the **Source** to the to do class
* [ ] Set **Run Credentials** to **Viewer**
* [ ] Set **Edit Credentials** to **Editor**
* [ ] Set the **Name** to `Completed Tasks`

In the **Fields Tab** :&#x20;

* [ ] Add all fields
* [ ] Order the **CreatedOn** field by Descending Order

![Completed Tasks Quey Properties](/files/-MABdqcG6BKtsD0ac0WI)

In the **Conditions Tab**, we click on **Add Condition** set the condition to `MyToDos.IsCompleted = 1`

![Completed Tasks Conditions Properties](/files/-MAXnBF8nz_zYRZcS3DJ)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tivity.one/overview/getting-started/creating-queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
