> For the complete documentation index, see [llms.txt](https://help.felt.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.felt.com/upload-anything/sql-queries.md).

# SQL queries

Turn SQL query results from a connected database into map layers. Write the query yourself or ask Felt AI for help.

{% hint style="success" %}
Available on the Business and Enterprise plans. [Compare plans](https://felt.com/pricing#compare)
{% endhint %}

Query a connected database with SQL to turn its results into a map layer. Write the query in the SQL editor or ask Felt AI to write it. Refresh the layer manually or on a schedule to update its results.

This page covers SQL on connected databases. To query layers already on your map, ask Felt AI. See [AI assistant](/felt-ai/ai-assistant.md).

## Create a layer from a SQL query

SQL queries work with [Postgres / PostGIS](/upload-anything/data-sources/postgres-postgis.md), [Snowflake](/upload-anything/data-sources/snowflake.md), [BigQuery](/upload-anything/data-sources/bigquery.md), [Databricks](/upload-anything/data-sources/databricks.md), [Amazon Redshift](/upload-anything/data-sources/amazon-redshift.md), [Microsoft SQL Server](/upload-anything/data-sources/microsoft-sql-server.md), and [Wherobots](/upload-anything/data-sources/wherobots.md) sources. The results must include a geometry column so Felt can draw the features.

1. Click **Data sources** in the toolbar, then select your database source.
2. Click **New SQL query** at the top of the source's **Recent queries**. To start from one table, click the **···** menu on the table and click **Write SQL query**.
3. Write your query in the editor, or describe what you want in the **Felt AI** tab at the bottom of the editor. Felt AI writes the SQL in the editor, where you can review and change it.
4. Click **Preview**. The **Results** tab shows up to 100 rows.
5. Optional: To make the layer refresh on a schedule, click the arrow next to **Create layer**, turn on **Live**, and select a frequency.
6. Click **Create layer**. The layer appears on the map, ready to style.

Felt runs only `SELECT` statements, so a query can't change or delete data in your database. Edits you make to the layer on the map stay in Felt.

To reuse a query, select it in the source's **Recent queries**. The list shows the 20 most recent queries used to create layers from the source, including other people's queries.

For what to ask Felt AI, see [Prompting tips](/felt-ai/prompting-tips.md). For the spatial functions your database supports, see its documentation: [PostGIS](https://postgis.net/docs/reference.html), [Snowflake](https://docs.snowflake.com/en/sql-reference/functions-geospatial), [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions), [Databricks](https://docs.databricks.com/en/sql/language-manual/sql-ref-h3-geospatial-functions.html), [Amazon Redshift](https://docs.aws.amazon.com/redshift/latest/dg/geospatial-functions.html), or [Microsoft SQL Server](https://learn.microsoft.com/en-us/sql/relational-databases/spatial/spatial-data-sql-server?view=sql-server-ver16).

<figure><img src="https://217108486-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmRfGitkyjOEMvVsEyGWN%2Fuploads%2Fgit-blob-55ec7ffef64d70e928c8fb16fce94375bd6731c7%2Fsql-editor-ai-assistant.png?alt=media" alt="SQL editor with a query on Seattle census block groups, and the Results tab showing rows of block group data."><figcaption></figcaption></figure>

## Refresh a layer from a SQL query

Refreshing a layer from a SQL query reruns the query, so the layer picks up changes in the database. Between refreshes, its results stay the same.

* To refresh the layer now, select the layer, open its **Data** tab, and click **Refresh**.
* To refresh the layer on a schedule, turn on **Live** in the **Data** tab and select a frequency.

A refresh replaces edits made to the layer on the map. Each refresh counts toward your workspace's processing limit. See [Refresh data](/upload-anything/refresh-data.md).

## Edit a layer's SQL query

You can change the SQL query of a layer created from a SQL query at any time, including one Felt AI created from a database source. You need Edit access to the source. A layer Felt AI made by querying map layers has no SQL editor, so ask Felt AI to change it.

1. In edit mode, select the layer in the legend.
2. In the detail panel, open the **Data** tab.
3. Next to **Dataset**, click **SQL query**. The SQL editor opens with the layer's query.
4. Change the query yourself, or ask Felt AI to change it.
5. Click **Preview** to check the results.
6. Click **Save**. If the layer has edits made on the map, Felt warns that the new query replaces them. Click **Update query** to continue.

Felt reprocesses the layer with the new query and keeps its styling.

## Fix a query that fails

A SQL query that fails shows *There was an issue running this query.* in the **Results** tab, with the database's error message. Click **Fix errors with AI** to have Felt AI correct the query, or fix it yourself, then click **Preview** again.

## Frequently asked questions

<details>

<summary>Why don't I see New SQL query on my source?</summary>

The SQL editor appears only on database sources, and only for people with Edit access to the source. Buckets, web services, and Felt Servers don't support SQL queries. If it's a database source, ask someone with Source admin access to give you Edit access. See [Permissions](/administration/permissions.md).

</details>


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.felt.com/upload-anything/sql-queries.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
