# Running queries from Builder

Once you have created your BigQuery connection in the CARTO Workspace (see [*Getting Access*](https://docs.carto.com/data-and-analysis/getting-access#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`](https://docs.carto.com/analytics-toolbox-for-redshift/sql-reference/clustering#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="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-d365844b8c8720956f59892c1e5b0daa1bf1b96a%2Fthe_tileset_layer_choosing_connection.png?alt=media&#x26;token=568375b9-b6b9-492a-9a09-29375c225f37" 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="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-7cb82142f54e6ae09b30099fdda06c747607d4dc%2Fthe_builder_custom_query_option.png?alt=media&#x26;token=19517417-d393-4c1c-b9f3-6c325241453a" alt=""><figcaption></figcaption></figure>
3. Click on Add source. A SQL console will be displayed.

   <figure><img src="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-c6050b71f16f69ac2cdfb158048eff22914bb7f3%2Fthe_builder_custom_query_console.png?alt=media&#x26;token=6e70e386-beb9-403d-852f-6c1877b51d38" 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="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-437189477053df3cea5def29f09c3ed040839610%2Fthe_map_custom-query-analytics-toolbox-clustering.png?alt=media&#x26;token=9a1072a2-08af-47f2-b399-d40434a10d60" 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](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-postgresql/sql-reference).
{% endhint %}

<img src="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-1c82685e4e434438152a8e3d867df996413489fe%2Feu-flag-website.png?alt=media&#x26;token=4343f6e5-973a-4e9a-8e14-50366a086f72" 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.
