> For the complete documentation index, see [llms.txt](https://docs.carto.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/guides/running-queries-from-builder.md).

# Running queries from Builder

Once you have created your BigQuery connection in the CARTO Workspace (see [*Getting Access*](/data-and-analysis/analytics-toolbox-for-bigquery/getting-access.md#access-from-carto-workspace) for details), you can create custom SQL layers in Builder that make use of the Analytics Toolbox for BigQuery.

{% hint style="info" %}
**From the CARTO Data Warehouse connection**

You can also run queries from Builder using the Analytics Toolbox for BigQuery from your CARTO Data Warehouse connection. This connection is available and ready-to-use for all CARTO accounts.
{% endhint %}

The Analytics Toolbox functions are available from the `carto-un` and `carto-un-eu` BigQuery projects. These projects are deployed in the US and EU multi-regions, respectively, and you may choose one or the other depending on the location of your data.

To get started, let’s run a simple example query to cluster a set of points using the [`ST_CLUSTERKMEANS`](/data-and-analysis/analytics-toolbox-for-redshift/sql-reference/clustering.md#st_clusterkmeans) function.

1. Click on the *Add source from* button in Builder, that can be found at the bottom left of the screen.

   <figure><img src="/files/za1U6JZ8el7oPcvjLt9l" alt=""><figcaption></figcaption></figure>
2. Select the second tab Custom Query (SQL) and pick the BigQuery or CARTO Data Warehouse connection that you will use to run the query.

   <figure><img src="/files/fiY6hWqZGQYfN3FIHW9D" alt=""><figcaption></figcaption></figure>
3. Click on Add source. A SQL console will be displayed.

   <figure><img src="/files/v62jUvkFlfhn0lke0r75" alt=""><figcaption></figcaption></figure>
4. Copy and paste the following query:

{% tabs %}
{% tab title="carto-un" %}

```sql
with clustered_points AS
(
    SELECT `carto-un`.carto.ST_CLUSTERKMEANS(ARRAY_AGG(geom ignore nulls), 6) AS cluster_arr
    FROM carto-demo-data.demo_tables.sample_customer_home_locations
)
SELECT cluster_element.cluster, cluster_element.geom AS geom FROM clustered_points, UNNEST(cluster_arr) AS cluster_element;
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
with clustered_points AS
(
    SELECT `carto-un-eu`.carto.ST_CLUSTERKMEANS(ARRAY_AGG(geom ignore nulls), 6) AS cluster_arr
    FROM carto-demo-data.demo_tables.sample_customer_home_locations
)
SELECT cluster_element.cluster, cluster_element.geom AS geom FROM clustered_points, UNNEST(cluster_arr) AS cluster_element;
```

{% endtab %}

{% tab title="manual" %}

```sql
with clustered_points AS
(
    SELECT carto.ST_CLUSTERKMEANS(ARRAY_AGG(geom ignore nulls), 6) AS cluster_arr
    FROM carto-demo-data.demo_tables.sample_customer_home_locations
)
SELECT cluster_element.cluster, cluster_element.geom AS geom FROM clustered_points, UNNEST(cluster_arr) AS cluster_element;
```

{% endtab %}
{% endtabs %}

1. Run the query. This query computes five clusters from the points of the sample\_customer\_home\_locations table. As a result, each point is assigned a cluster ID.
2. Style the layer by the cluster attribute.

<figure><img src="/files/QTZuNLZNZ9G7njvPjyW8" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
For more examples, visit the [Examples](https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials) section in our Academy or try executing any of the queries included in every function definition in the [SQL Reference](/data-and-analysis/analytics-toolbox-for-postgresql/sql-reference.md).
{% endhint %}

<img src="/files/4m1BK9j4Wq34gat4HHd2" alt="EU flag" data-size="line"> This project has received funding from the [European Union’s Horizon 2020](https://ec.europa.eu/programmes/horizon2020/en) research and innovation programme under grant agreement No 960401.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.carto.com/data-and-analysis/analytics-toolbox-for-bigquery/guides/running-queries-from-builder.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
