# Running and saving CQL queries

Learn about CQL basics and CQL tools (Query builder and CQL explorer) in [Cortex Query Language (CQL)](https://docs.cortex.io/standardize/cql).

## Running a CQL query

The [Query builder](https://docs.cortex.io/standardize/cql/..#the-query-builder-tool) allows you to define your query without needing to learn CQL upfront.

You will need the `Run query builder` permission. If you are running queries on third-party integrations, you will also need the `Run query builder with third-party integrations` permission.

### Step 1: Build a query

1. On the right side of the Query builder page, click the **CQL builder** tab. \\

   <figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-7d12024e8e6bc90b45b9aea3918b9aaca984ac3c%2Fcql-builder-tab.jpg?alt=media" alt="CQL builder is on the right side of the Query builder page."><figcaption></figcaption></figure>
2. In the CQL builder, choose and integration and a rule to evaluate.
   * The rules available in the dropdown menu will depend on the integration you've selected.
   * Depending on the rule you choose, you may need to configure additional fields.
3. Click **Use query**. The query will automatically populate into the CQL text editor on the left side of the page.\\

   <div align="left"><figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-6a980f625c08cf4f514c6c7761845090b7dd48df%2Fuse-query.jpg?alt=media" alt="Click &#x22;Use query&#x22; in CQL builder, then the query populates the CQL search field." width="563"><figcaption></figcaption></figure></div>

### Step 2: Test the query

1. Under the CQL text editor on the left, select an entity to test the query against.
2. Click **Test**.
   * The results appear under the text editor. Review the results to verify that the query is working as you expect.\
     ![The CQL query test results appear under the text editor.](https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-c3658a357820a3995c650191672a0e21c7b28a3e%2Ftest-query.jpg?alt=media)\\

### Step 3: Run the query

1. In the lower right corner of the page, click **Run query**.
2. In the side panel that opens, choose whether to run the query on all entities or select specific entities.\
   ![Choose which entities to run the query on](https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-67c963473b38325047936e05629a12839aeacc2e%2Frun-query-panel.jpg?alt=media)
3. At the bottom of the side panel, click **Run query**.
4. In the confirmation modal that appears, click **Yes, run query**.

### View and share results

After running the query, the page displays a list of all entities matching the criteria. In the upper right corner of the list, you can sort and filter the list. As you apply filters to your list, Cortex will also update the number of matching entities, so you can easily see at a glance how many entities match your requirements.

You can share the results in two ways:

* **Send a link**: Click **Share** in the upper right corner of the results list to copy the URL to your clipboard. You can share the link with anyone who has access in your Cortex workspace.
* **Export as CSV**: In the upper right corner of the page, click **Export CSV** to download a CSV file of the data.

### Running rules on multiple queries

If you want to run a query on more than one rule, you can join multiple queries together with `AND` and `OR`.

For example:

```
git.commits().length == 2 AND azureDevops.workItems().length > 4
```

### CQL errors

The following errors may occur when running a CQL query:

* `400`: This typically indicates a misconfiguration with an integration. For example, you may be missing an entity registration required for an integration.
* `403`: This occurs if there are missing or improper [permissions](https://docs.cortex.io/configure/settings/managing-users/permissioning).
* `429`: This occurs when hitting the rate limit for an integration. Cortex will retry 5 times before responding with this error. To prevent rate limit issues, we have built a self-throttling system that proactively throttles before hitting a rate limit from the vendor.
* `500`: This indicates that the integration itself returned an error.

## Save a query

While viewing the results of a query you ran, you can save the query to use again in the future:

1. In the upper right corner of the results page, click **Save query**.\\

   <figure><img src="https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-19e3489ebc4274b953f31c01730e92c2ec8f53a4%2Fsave-query.jpg?alt=media" alt="Click &#x22;Save query&#x22; in the upper right."><figcaption></figcaption></figure>
2. In the side panel, configure the query details:
   * Enter a name and description for your query.
   * To allow other users in your Cortex workspace to see the query, enable the toggle next to **Share across organization**.
3. Click **Save**.

## View active, recent, and saved queries

Below the CQL search text box, you can see active queries. This section displays the ongoing process of your submitted query. When the query is complete, it will appear under **Recent**.

Along the top of the query builder, you can click into tabs to view **Saved** and **Recent** queries. Click into any of the queries in these lists to view the results of the query.\
![Query builder has tabs for Search, Saved, and Recent.](https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-3b749c18804e25fe6fcaab662ff0c304844f867b%2Fquery-builder-tabs.jpg?alt=media)

### Saved queries

Click the **Saved** tab to view a list of your saved queries, and queries that others have saved and shared across your organization.

Query results are not automatically updated, but you can refresh a query manually: While viewing the results page, click the 3 dots icon, then click **Refresh**.\
![Click the 3 dots icon then click Refresh.](https://826863033-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJW7pYRxS4dHS3Hv6wxve%2Fuploads%2Fgit-blob-bf852f64bfd56349f0c78b3dd2d33d59a5a78cee%2Frefresh-query.jpg?alt=media)

{% hint style="success" %}
When configuring a [CQL report](https://docs.cortex.io/standardize/cql-reports#enable-auto-refresh-for-cql-reports), it is possible to enable automatic refresh.
{% endhint %}

### Recent queries

Click the **Recent** tab. This list shows all queries that have been run in the last 30 days.
