# Database Queries

In Zingy, you can define your own Database Queries associated with [Database Tables](https://docs.zingy.ai/app-editor/data/database-tables).

Database Queries enable you to access the table's data with specific criteria, allowing your app to retrieve data quickly and efficiently.

## Managing Database Queries

To manage the queries for a Database Table, click on the table and select the **`Queries`** option from the popup menu.

<div align="left"><figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2F5pSSKrcm3GIYFjfAgU7p%2Fimage.png?alt=media&#x26;token=436846c9-f124-4eff-a355-99845eae624e" alt="" width="375"><figcaption><p>Table menu: Queries</p></figcaption></figure></div>

An example of the Database Query screen is shown in the image below.&#x20;

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FStyTa9GiSJFVXEHSpvaJ%2Fimage.png?alt=media&#x26;token=9f124858-fb41-49ee-bf67-e70e27186abf" alt=""><figcaption><p>Database Query screen showing the Add New button <mark style="color:red;"><strong>(1)</strong></mark>, the query listing <mark style="color:red;"><strong>(2)</strong></mark>, popup menu with options <mark style="color:red;"><strong>(3)</strong></mark>.</p></figcaption></figure>

In the above image, the Database Query screen lists all the currently saved queries <mark style="color:red;">**(2)**</mark> and allows:

* Addition of new Queries <mark style="color:red;">**(1)**</mark>
* Editing Queries <mark style="color:red;">**(3)**</mark>
* Deleting Queries <mark style="color:red;">**(3)**</mark>

***

## Query Editor

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2F9GOcs00McO9IkuClwQWK%2Fimage.png?alt=media&#x26;token=6a7c1aed-159e-4a60-962f-a5f4c931e81c" alt=""><figcaption><p>Database Query editor showing the Query Name <mark style="color:red;"><strong>(1)</strong></mark>, Search Criteria tab <mark style="color:red;"><strong>(2)</strong></mark>, Field Selection tab <mark style="color:red;"><strong>(3)</strong></mark>, Sorting Order tab <mark style="color:red;"><strong>(4)</strong></mark></p></figcaption></figure>

The Query Editor allows you to create new queries or edit existing ones. It consists of the following (refer to the image above):

* Query Name <mark style="color:red;">**(1)**</mark> : Specify the unique name of the query. Please note that the query name cannot be changed after the query is created.
* [Search Criteria tab](#search-criteria) <mark style="color:red;">**(2)**</mark>
* [Field Selection tab](#field-selection) <mark style="color:red;">**(3)**</mark>&#x20;
* [Sorting Order tab](#sorting-order) <mark style="color:red;">**(4)**</mark>

### Search Criteria

The search criteria consists of a match expression.  The expression can be something as simple as <mark style="background-color:yellow;">FirstName</mark> <mark style="color:purple;background-color:yellow;">**`equals`**</mark> <mark style="background-color:yellow;">Input-1</mark> , which will list records where the *FirstName* field exactly matches the provided input parameter (*Input-1*).

More complicated expressions with logical combinations (*AND*, *OR*, *NOT*) can also be constructed (more on this topic later in this page).

#### Adding a match expression

If you are adding your first match expression for the query, click on the button with the :heavy\_plus\_sign: icon <mark style="color:red;">**(1)**</mark> as shown below.  This will bring up the Match Expression editor.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2F8bWWmrKG65oSiC6fY8i1%2Fimage.png?alt=media&#x26;token=c17f5a00-fb8b-420c-a151-c4e508255ad4" alt=""><figcaption><p>Adding the first match expression <mark style="color:red;"><strong>(1)</strong></mark></p></figcaption></figure>

The image below shows the Match Expression editor.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FEKNlfum7Kp4GzNiZlqZS%2Fimage.png?alt=media&#x26;token=4fe11fff-b5ce-4f53-9a4b-2d9e1a348884" alt=""><figcaption><p>Match Expression editor showing the Field selection <mark style="color:red;"><strong>(1)</strong></mark>, Condition selection <mark style="color:red;"><strong>(2)</strong></mark> and the Input parameter selection <mark style="color:red;"><strong>(3)</strong></mark></p></figcaption></figure>

The match expression is created as follows:

* **Field&#x20;**<mark style="color:red;">**(1)**</mark>: Choose one of the fields in the [Database Table](https://docs.zingy.ai/app-editor/data/database-tables). The *ID* field is an automatic internal field that is part of every table. it provides an internally unique identifier for each record. The field listing will also show the *ID* field.
* **Condition&#x20;**<mark style="color:red;">**(2)**</mark>: Choose one of the conditions for the match. The conditions shown are based on the [type](https://docs.zingy.ai/app-editor/database-tables#field-types) of the field selected.

<table><thead><tr><th width="128">Condition</th><th>Description</th></tr></thead><tbody><tr><td>=</td><td>Checks if the field's value is equal (exact match) to the provided input parameter. Works for all field <a href="../database-tables#field-types">types</a>.</td></tr><tr><td>!=</td><td>Checks if the field's value is not equal (not an exact match) to the provided input parameter. Works for all field <a href="../database-tables#field-types">types</a>.</td></tr><tr><td>></td><td>Works for Numeric, Numeric (decimals) and Large Numeric field <a href="../database-tables#field-types">types</a>. Checks if the field's value is greater than the provided input parameter.</td></tr><tr><td>&#x3C;</td><td>Works for Numeric, Numeric (decimals) and Large Numeric field <a href="../database-tables#field-types">types</a>. Checks if the field's value is less than the provided input parameter.</td></tr><tr><td>>=</td><td>Works for Numeric, Numeric (decimals) and Large Numeric field <a href="../database-tables#field-types">types</a>. Checks if the field's value is greater than or equal to the provided input parameter.</td></tr><tr><td>&#x3C;=</td><td>Works for Numeric, Numeric (decimals) and Large Numeric field <a href="../database-tables#field-types">types</a>. Checks if the field's value is less than or equal to  the provided input parameter.</td></tr><tr><td>matches pattern</td><td><p>Works for the Text field <a href="../database-tables#field-types">type</a>.</p><p>Does a <a href="https://en.wikipedia.org/wiki/Regular_expression">regular expression</a> match (case insensitive global search) of the field's value with the provided input parameter.</p></td></tr></tbody></table>

* Input parameter <mark style="color:red;">**(3)**</mark> to use for the match. You can either choose an existing parameter or add a new one.

Here are some examples of match expressions:

* &#x20;<mark style="background-color:yellow;">Price</mark>   <mark style="color:purple;background-color:yellow;">**`is less than`**</mark>  <mark style="background-color:yellow;">Input-1</mark>&#x20;

  The value of the *Price* field is compared to the *Input-1* parameter. If the value is less than the parameter, the record is output.
* &#x20;<mark style="background-color:yellow;">Email</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-1</mark>&#x20;

  The *Input-1* parameter is used as a [regular expression](https://en.wikipedia.org/wiki/Regular_expression). A case insensitive global search is conducted on the value of the *Email* field. If the match succeeds, the record is output.

#### Logical Combinations

You can logically combine multiple match expressions using the boolean *AND* , *OR* and *NOT* operations.&#x20;

<div><figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FB0NoiMK1uCyDNDKEPBUM%2Fimage.png?alt=media&#x26;token=80198e17-68b7-461a-886a-fcb6059887fe" alt=""><figcaption><p>The logical combination button <mark style="color:red;"><strong>(1)</strong></mark></p></figcaption></figure> <figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FjKmF3EW0wPIxfl4r1u5T%2Fimage.png?alt=media&#x26;token=f6ecd054-c044-41f2-b50e-a01239e9b349" alt=""><figcaption><p>The logical combination popup menu <mark style="color:red;"><strong>(2)</strong></mark></p></figcaption></figure></div>

Clicking on the logical combination button <mark style="color:red;">**(1)**</mark> will bring up a popup-menu showing options for the logical combination operations <mark style="color:red;">**(2)**</mark>.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FWrd0JQIOtEiKVi9KN35u%2Fimage.png?alt=media&#x26;token=2e3c3193-20f2-48d8-8060-f869e8503b7f" alt=""><figcaption><p>The selected expression <mark style="color:red;"><strong>(2)</strong></mark> after the OR group was chosen is now shown inside the OR expression <mark style="color:red;"><strong>(1)</strong></mark></p></figcaption></figure>

After selecting the logical combination, the chosen match expression will be displayed as nested inside the selected logical combination group, as illustrated in the above image.

You can now add more match expressions to the logical combination group by clicking in the :heavy\_plus\_sign: icon <mark style="color:red;">**(1)**</mark> as shown below. This will bring up the [Match Expression editor](#adding-a-match-expression).

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FnWJ3IaBxRjriuonIgrTK%2Fimage.png?alt=media&#x26;token=665021ad-9c6a-4c6c-9336-1f3fb8b71e2c" alt=""><figcaption><p>Adding a match expression to a logical combination group <mark style="color:red;"><strong>(1)</strong></mark>. Combining one logical group into another <mark style="color:red;"><strong>(2)</strong></mark>.</p></figcaption></figure>

Just like a match expression, a logical combination group can also be grouped into another logical combination group using the the logical combination button <mark style="color:red;">**(2)**</mark> shown in the above image.

Examples of logical combination groups are provided below:

* ![](https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FXk1DR30SbCh8xLMPwodL%2Fimage.png?alt=media\&token=d2457eba-309e-49a4-b582-fbb648df36ce)

  In this example, two match expressions,  <mark style="background-color:yellow;">Email</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-1</mark>   and   <mark style="background-color:yellow;">FirstName</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-1</mark>  are joined using an ***OR*** group.

  This will lead to the output of records where either the *Email* or the *FirstName* field's values correspond with the regular expression search using the *Input-1* parameter.

* ![](https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FltashX6KUsNsYBQN5JeW%2Fimage.png?alt=media\&token=ab9ba1e2-5085-45e6-a616-7f851a12bab8)

  Here, the two match expressions,  <mark style="background-color:yellow;">Price</mark>   <mark style="color:purple;background-color:yellow;">**`is greater than`**</mark>  <mark style="background-color:yellow;">Input-1</mark>   and   <mark style="background-color:yellow;">Name</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-2</mark>  are merged using an ***AND*** group.&#x20;

  This will lead to the output of records where the *Price* field's value exceeds the *Input-1* parameter and the *Name* field's value aligns with the regular expression search using the *Input-2* parameter.

* ![](https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FcZQ0RwDjFXgR2IY1vQCt%2Fimage.png?alt=media\&token=1ee31e11-f641-458e-9963-7fda5c2c8913)

  In this example, an ***AND*** group contains an ***OR*** group and the match expression <mark style="background-color:yellow;">PhoneNumber</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-2</mark>  . The ***OR*** group includes two match expressions: <mark style="background-color:yellow;">FirstName</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-1</mark> and  <mark style="background-color:yellow;">LastName</mark>   <mark style="color:purple;background-color:yellow;">**`matches pattern specified in`**</mark>  <mark style="background-color:yellow;">Input-1</mark> .

  This will result in the output of records where the *PhoneNumber* field's value aligns with the regular expression specified in *Input-2*, ***AND*** either the *FirstName* or *LastName* fields correspond with the regular expression search using the *Input-1* parameter.

### Field Selection

Typically, when reading from the app's database, all the fields of the table are displayed. However, the Field Selection tab enables you to select only the specific fields you want to be outputted by the query.

The image below shows the Field Selection tab with no fields selected. The table's fields are shown with a :heavy\_plus\_sign: icon. The [*ID*](https://docs.zingy.ai/app-editor/database-tables#id-field) field is an automatic internal field that is part of every table. it provides an internally unique identifier for each record. The field listing will also show the [*ID*](https://docs.zingy.ai/app-editor/database-tables#id-field) field.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FOjTwGaBsd8idsHoYqwGB%2Fimage.png?alt=media&#x26;token=683fe31c-44a4-4b13-93db-fef767c84491" alt=""><figcaption><p>The Field selection tab with no specific fields selected.</p></figcaption></figure>

To select a field, click on the :heavy\_plus\_sign: icon. The image below shows an example where two fields *FirstName* and *PhoneNumber* are selected. Selecting a field moves it out of the field listing and shows it in the selected section.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FcuEGSobcwK5uprkyBUkK%2Fimage.png?alt=media&#x26;token=bb20a66a-598a-47c0-b79d-e9648d267d7d" alt=""><figcaption><p>Example where two fields are selected.</p></figcaption></figure>

To deselect a field click on the 'x' icon. This will move the field from the selected section to the field listing.

### Sorting Order

By default, when you read from the app's database, records are listed in an internally determined order which typically is based on the time the record was created.

You can customize this behavior using the Sorting Order tab.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FwEkTVStoM6qzQND5k3eo%2Fimage.png?alt=media&#x26;token=88bd587c-98ea-404d-b075-d1c2bdd37b74" alt=""><figcaption><p>The Sorting order tab with no fields selected.</p></figcaption></figure>

To add a field, click on the :heavy\_plus\_sign: icon and select the type (Ascending or Descending). You can sort using multiple fields, in which case the sorting will take place in the order you added them. If two records have the same values for a field, the next field in the sorting list will be assessed, and so on.

The image below shows an example where two fields *FirstName (ascending)* and *PhoneNumber (descending)* are selected. Selecting a field moves it out of the field listing and shows it in the selected section.

<figure><img src="https://3401585094-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmnBepgycwgisXr9ya1N4%2Fuploads%2FDqOkLca3efPw0mAd47Dd%2Fimage.png?alt=media&#x26;token=e57ffd12-b213-4a7f-aeee-0abd237c6f0a" alt=""><figcaption><p>Example where two fields are selected for sorting.</p></figcaption></figure>

In the above example, the records will be sorted by *FirstName* field in ascending order. Records with the same *FirstName*, will be sorted by *PhoneNumber* in descending order.

To deselect a field click on the 'x' icon. This will move the field from the selected section to the field listing.
