> For the complete documentation index, see [llms.txt](https://docs.octoxlabs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.octoxlabs.com/documentation/use-octoxlabs/queries/create-and-save-queries.md).

# Create and Save Queries

Build a query in the Query Wizard or the search box, check the result and save it for reuse.

Write a query in an inventory, run it, and save it as a named query that you can run again or reuse in charts, reports, profiles, policies and workflows.

### Before you begin

* Required permissions: `queries.add_query` to save a query (without it, **Save As** is hidden), `queries.view_query` to open the **Saved Queries** page, and the view permission of the inventory you query.
* At least one completed discovery, so that you have data to test against.
* Saving is available in the inventories, not in the Asset Explorer.

### Steps

{% stepper %}
{% step %}

#### Open an inventory

In the left menu, select **Inventories** and then the inventory you want to query, for example **Devices**. The same steps work in **Users**, **Software**, **Vulnerabilities**, **Licenses**, **Certificates**, **Services**, **Source Codes**, **AI Services**, **Domains** and **Policies**.
{% endstep %}

{% step %}

#### Write the query

You have three ways to write a query. Use any of them, and combine them freely: the **Query Wizard** reads the text in the search box and shows it as rows.

* **Search box.** Type the query and press Enter. Use this when you know the syntax. See the OctoXLabs Query Reference.
* **Query Wizard.** Select **Query Wizard**. Each row is one condition, described in the next section.
* **Query AI.** If your account can use it, select **Query** and describe what you are looking for. The assistant returns a query for the search box.

IMAGEHERE: Query Wizard open on the Devices inventory with three rows (one with NOT, one with the AND/OR selector, one nested row), and the Cancel and Run buttons.
{% endstep %}

{% step %}

#### Run and check the result

In the Query Wizard, select **Run**. The table shows the matching assets, and the search box now holds the query text. Adjust the query until the result is what you expect.
{% endstep %}

{% step %}

#### Save the query

Select **Save As** next to the search box. In the **Save Query** dialog, enter a **Query Name**, decide whether to select **Public**, and select **Save**. A message confirms "Query saved as" followed by the name.

If the search box is empty, OctoXLabs shows "Query is empty." instead of opening the dialog.

IMAGEHERE: The Save Query dialog with the Query Name field and the Public checkbox.
{% endstep %}
{% endstepper %}

### Query Wizard rows

Each row of the Query Wizard becomes one condition of the query text. Rows are joined in order, and you can drag a row by its handle to move it.

| Control            | What it does                                                                                                                                                 |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **AND** / **OR**   | Joins the row to the one above it. The first row has no joiner.                                                                                              |
| **(** and **)**    | Opens or closes a group. Each press adds one bracket, up to three, and another press clears them. Use groups when you mix **AND** and **OR**.                |
| **NOT**            | Reverses the condition.                                                                                                                                      |
| Adapter            | **OctoXLabs** for the fields every adapter shares, or a single adapter for its own fields. Adapter fields are written as `Specifics.<adapter-slug>.<field>`. |
| Field              | The field to test. The choices depend on the adapter you picked.                                                                                             |
| Operator           | The comparison. The list depends on the field type, see the table below.                                                                                     |
| Value              | The value to compare with. The input changes with the field type: text, number, date picker, yes or no, adapter list, tag list or saved query list.          |
| **Add Row**        | Adds another condition.                                                                                                                                      |
| **Add Nested Row** | Adds a block for a field that holds a list of objects, such as installed software. Every condition in the block must match the same list item.               |

#### Operators

| Wizard operator                                                          | Query text              | Field types                      |
| ------------------------------------------------------------------------ | ----------------------- | -------------------------------- |
| equal                                                                    | `=`                     | Text, number, date               |
| contains                                                                 | `~`                     | Text                             |
| startswith                                                               | `startswith`            | Text                             |
| endswith                                                                 | `endswith`              | Text                             |
| regex                                                                    | `RE //pattern//`        | Text fields that support regex   |
| greater than, greater than or equal to, less than, less than or equal to | `>` `>=` `<` `<=`       | Number, date                     |
| last                                                                     | `last`                  | Date                             |
| last minute                                                              | `lastm`                 | Date                             |
| next                                                                     | `next`                  | Date                             |
| in                                                                       | `IN [[value1, value2]]` | Any field                        |
| is                                                                       | `is EMPTY` or `is ANY`  | Any field outside a nested block |

To reuse a saved query inside a new one, pick the field **Query** with the operator **is**, and then choose the saved query. The text it writes is `Query is <query ID>`. For what each operator does, see the OctoXLabs Query Reference.

{% hint style="info" %}
The operators **in**, **regex** and **is** (**Empty** and **Any**) were added in OctoXLabs 6.7.0. On older versions, write the same conditions with the operators in the reference.
{% endhint %}

### Public and private queries

The **Public** checkbox is cleared by default, so a new query is private and only you can see and run it. A public query is visible to everyone who can open the **Saved Queries** page. Public queries can be changed by anyone with the `queries.change_query` permission. A private query can be changed only by its owner. See Modify Existing Queries.

### What a saved query keeps

* **Columns and sorting.** **Save As** stores the columns and the sort order your table has at that moment. They are applied only when you run the query with the **Run with saved columns & sorting** button, see Modify Existing Queries. A plain run keeps your own columns.
* **Count.** The number of matching assets in the latest completed discovery, calculated when you save and again each time you edit the query. It is shown in the **Count** column of the **Saved Queries** page.
* **Query ID.** A short ID that OctoXLabs generates. It is shown in the **ID** column of the **Saved Queries** page. Use it in `Query is <query ID>`.

### Verify the result

In the left menu, select **Inventories** > the inventory > **Saved Queries**. Your query is in the list with its **Name**, **Public** flag and **Count**.

### Troubleshooting

| Symptom or message                                                  | Cause                                                                                                       | Fix                                                                         |
| ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| "Query is empty."                                                   | **Save As** was selected with an empty search box.                                                          | Type or build a query first.                                                |
| "Invalid query syntax. Please check your query text and try again." | The text could not be parsed, for example brackets that do not match.                                       | Check that every ( has a closing ).                                         |
| "Please check your query text"                                      | The inventory could not run the query.                                                                      | Check the syntax and the field names against the OctoXLabs Query Reference. |
| "Query not found. It's may be deleted." followed by a query ID      | The query uses `Query is <ID>` for a saved query that was deleted.                                          | Edit the query and remove or replace that condition.                        |
| The query runs but returns nothing, or the wrong assets.            | An unsupported operator such as `!=` or `contains` turns the text into a full-text search without an error. | Use `NOT` and `~`. See the warning in the OctoXLabs Query Reference.        |
| **Save As** is missing.                                             | Your account lacks `queries.add_query`, or you are in the Asset Explorer.                                   | Ask an administrator, or save from an inventory.                            |

### What's next?

<table data-view="cards"><thead><tr><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Modify Existing Queries</strong></td><td>Run, edit, copy, organize and delete saved queries.</td><td></td></tr><tr><td><strong>Query Examples</strong></td><td>Copy ready-made queries for common questions.</td><td></td></tr><tr><td><strong>Create a Dashboard Chart</strong></td><td>Show a saved query as a chart.</td><td></td></tr><tr><td><strong>OctoXLabs Query Reference</strong></td><td>Look up operators, fields and syntax.</td><td></td></tr></tbody></table>
