# Running queries from Builder

Once you have created your Postgres connection in the CARTO Workspace (see [*Getting Access*](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-postgresql/overview/getting-started) for details), you can create custom SQL layers in Builder that make use of the Analytics Toolbox for Postgres.

To get started, let’s run a simple example query to subdivide the area into Quadbin grid cells using the [`QUADBIN_POLYFILL`](https://docs.carto.com/data-and-analysis/analytics-toolbox-for-postgresql/sql-reference/quadbin#quadbin_polyfill) function from the *quadbin* module.

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-4f86693625d82a3f0edc47610fc75d18dc5caf57%2Flayer_choose_sf_connection.png?alt=media&#x26;token=75de1bb3-47d9-40fe-bda9-d1139048888d" alt=""><figcaption></figcaption></figure>
2. Select the second tab *Custom Query (SQL)* and pick the Postgres connection that you will use to run the query. Please make sure this connection has access to the Analytics Toolbox database.

   <figure><img src="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-00228bd5073924aa2fefef7e20424c89b585c85b%2Fbuilder_custom_query_option_pg.png?alt=media&#x26;token=dab1fe6e-774f-4901-a18c-3389a3ba51fe" 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-354735bead703391b7b77a39a0749d5553e1ac28%2Fbuilder_custom_query_console_sf.png?alt=media&#x26;token=1cf0f61f-ad8d-49a1-a1c2-8ca4209a0081" alt=""><figcaption></figcaption></figure>
4. Copy and paste the following query:

   ```sql
   WITH data AS(
   select 'New York' as city, ST_POINT(-74.00597, 40.71427) as geom 
   union all select 'Boston', ST_POINT(-71.05977, 42.35843) 
   union all select 'Philadelphia',ST_POINT(-75.16379, 39.95233) 
   union all select 'Washington',ST_POINT(-77.03637, 38.89511)
   )
   SELECT carto.QUADBIN_FROMGEOGPOINT((geom),9) as quadbin from data
   ```

   You can also take advantage of the function directly from a table from your database and combine it with other functions, such as the `QUADBIN_BOUNDARY` function, to get the geography boundary for a given quadbin:

   ```sql
   WITH data as (SELECT geom
   FROM mydatabase.myschema.table),
   quadbins as (
   SELECT carto.QUADBIN_POLYFILL(geom
   , 14) quadbins_polyfill from data
   )
   SELECT carto.QUADBIN_BOUNDARY(unnest(quadbins_polyfill)) as geom from quadbins
   ```
5. Run the first query. In this example we will showcase how to get the quadbin representationat at resolution level 9 for the geographic coordinates of four US cities dusing the `QUADBIN_POLYFILL` function.

<figure><img src="https://3029946802-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FybPdpmLltPkzGFvz7m8A%2Fuploads%2Fgit-blob-d4871a102985ec493e23f9815e29b5b36bd7da37%2Fbuilder_custom_query_quadbin_polyfill-pg.png?alt=media&#x26;token=1fdb889e-0b2e-4246-9426-3c396b304b96" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
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 %}
