For the complete documentation index, see llms.txt. This page is also available as Markdown.

SQL queries

Create dynamic map layers in Felt using SQL queries.

Felt’s SQL query feature lets you write and execute SQL queries directly against your database, enabling you to easily visualize results from custom filters, joins and spatial analysis powered upstream. It gives you full control over how your data is displayed, ensuring your maps are tailored to meet your specific requirements. Whether you're refining data or creating complex visualizations from generated SQL results, Felt makes it easy to seamlessly integrate your custom queries with your mapping workflow.

Set up

Connecting a cloud data source is required to access this feature, and SQL can only be run on the tables and views connected in your cloud source. The cloud sources that support SQL queries today are:

  • Postgres / PostGIS

  • Snowflake

  • BigQuery

  • Microsoft SQL Server

  • Databricks

  • Redshift

To set up a source connection, see detailed instructions on cloud sources.

Querying with Felt AI

Felt AI turns natural language questions into SQL for your connected database — anyone on the team can run complex spatial analysis, explore schemas and table attributes, and create live-syncing layers by describing what they want. The generated SQL always appears in the query editor, where you can inspect, edit, or extend it before creating a layer.

Perform complex spatial analysis

Explore data schema and table attributes

Optimize queries

Create a layer with a SQL query

  1. Click Data sources in the toolbar and select your data source under the Sources heading.

  2. Select + New SQL query (or select a table and click ··· > Write SQL query).

  3. The SQL query window will open

    1. the left side includes all the tables and views available in your source as well as recent queries the team has run

    2. the right side includes the SQL query editor (top) and the AI prompt area (bottom) where you can provide prompts to Felt AI. Use the Schema and Metadata tabs (bottom) to learn more about the structure of tables in the database.

  4. Once Felt AI generates SQL, a preview will run automatically. Use the Results tab (bottom right) to review results from the SQL query in table format.

  5. Click Create Layer to add your data to the map.

    1. The dropdown option allows you to set the Live sync Frequency or turn Live syncs off

  1. A new layer will be added to your map and you can start styling your results.

Editing SQL layers

You can review and edit existing layers with updated SQL either with or without AI assistance. You can edit or write queries directly within the query editor (top right).

To edit or update an existing layer built with SQL:

  1. Select the layer in Edit mode

  2. Navigate to the Data tab in the layer menu

  3. Click on SQL query next to Dataset

  1. Either edit the SQL directly or use Felt AI assistant to make modifications to the query

  2. Click Save to update the existing layer with the new query

Once edits are saved and while a layer is processing the updated query it cannot be modified or changed until it has finished the updates.

Copy recent queries

The 20 most recent SQL queries that created layers on maps will appear at the top when opening a source via Data sources. This will show you a history of all the queries run on maps in the workspace.

To re-use and fork queries other teammates have run, click on any of the entries listed under Queries and start editing.

Debugging

  1. Run previews by clicking on the Preview button (top right) to verify the SQL query is valid

  2. Errors will appear in the Results tab (bottom right) with more details

  3. You can fix errors with Felt's AI assistant by clicking the Fix errors with AI option

Supported spatial operations

Different SQL platforms offer varying levels of support for spatial operations, with some providing extensive libraries of geospatial functions and others offering more basic capabilities. While some focus on advanced geographic and geometric queries, others prioritize scalability and integration with broader data processing workflows. The specific capabilities and limitations depend on the platform, so refer to the relevant documentation linked below for more details.

The SQL query feature supports only SELECT statements, ensuring read-only access to your database.

Example operations

Spatial Operation
Definition
Code Example

ST_Intersects

Tests whether two geometries share any space in common. Returns true if geometries intersect, false otherwise. Used for identifying overlapping areas.

SELECT a.*, b.* FROM service_areas a JOIN market_zones b ON ST_Intersects(a.geometry, b.geometry);

ST_Within

Tests whether one geometry is completely inside another. Returns true if the first geometry is completely within the second geometry.

SELECT a.name, b.name FROM points a JOIN polygons b ON ST_Within(a.geometry, b.geometry);

ST_Buffer

Creates a new geometry that represents all points within a given distance from the input geometry. Useful for creating service areas or zones of influence.

SELECT location_id, ST_Buffer(geometry, 5000) as service_area FROM distribution_centers;

ST_Centroid

Computes the geometric center (centroid) of a geometry. Returns a point that represents the average position of all points in the geometry.

SELECT region_name, ST_Centroid(geometry) as center_point FROM service_regions;

ST_Distance

Calculates the shortest distance between two geometries. Returns the minimum distance between any two points in the two geometries.

SELECT a.id, b.id, ST_Distance(a.geometry, b.geometry) as distance FROM points a CROSS JOIN points b WHERE a.id != b.id;

ST_Area

Calculates the area of a polygon geometry. Returns the area in the units of the spatial reference system.

SELECT territory_id, ST_Area(geometry::geography) as area_sqm FROM market_territories;

ST_Length

Calculates the length of a linear geometry (linestring). Returns the length in the units of the spatial reference system.

SELECT route_id, ST_Length(geometry::geography) as length_meters FROM delivery_routes;

ST_Union

Combines multiple geometries into a single geometry. Dissolves overlapping boundaries and creates a single continuous geometry.

SELECT region_id, ST_Union(geometry) as combined_geometry FROM parcels GROUP BY region_id;

ST_Intersection

Returns the geometry representing the shared portions of two geometries. Creates a new geometry containing only the overlapping areas.

SELECT ST_Intersection(a.geometry, b.geometry) as overlap FROM zone_a a CROSS JOIN zone_b b;

ST_Transform

Transforms a geometry from one coordinate system to another. Used to convert between different spatial reference systems (SRS).

SELECT id, ST_Transform(geometry, 4326) as wgs84_geom FROM locations;

ST_Envelope

Computes the minimum bounding rectangle that contains a geometry. Returns a rectangular polygon that completely encloses the input geometry.

SELECT region_id, ST_Envelope(ST_Collect(geometry)) as bbox FROM market_regions GROUP BY region_id;

ST_ConvexHull

Computes the smallest convex polygon that contains all points in a geometry. Creates a shape that represents the outer boundary of a set of points.

SELECT cluster_id, ST_ConvexHull(ST_Collect(geometry)) as boundary FROM points GROUP BY cluster_id;

ST_Contains

Tests whether one geometry completely contains another. Returns true if the second geometry is completely contained within the first geometry.

SELECT points.* FROM customer_points points JOIN service_areas areas ON ST_Contains(areas.geometry, points.geometry);

Frequently asked questions

What are SQL queries in Felt?

SQL queries let you run spatial analysis against your cloud database from within Felt. You can ask questions in plain English — Felt AI generates the SQL and executes it against your database — or write the SQL yourself. Results are visualized on your map as layers.

Who can access SQL queries?

This feature is only available to admins and editors on the Enterprise plan. To upgrade, contact sales.

Do I need to know SQL to use this feature?

No, you can ask questions in plain English and Felt AI will generate the SQL code for you. However, you can also write or modify SQL directly if desired.

How do I review my query results?

Use the Results tab (bottom right) to review results from the SQL query in table format. A preview runs automatically once Felt AI generates the SQL.

Can I control how often my data syncs?

Yes, when clicking Create Layer, the dropdown option allows you to set the Live sync Frequency or turn Live syncs off.

How can I learn more about my database structure in Felt?

Use the Schema and Metadata tabs at the bottom of the interface to learn more about the structure of tables in the database.

What happens after I create a layer?

A new layer will be added to your map and you can start styling your results immediately.

Are queries previewed automatically?

Yes, once Felt AI generates SQL, a preview will run automatically so you can review the results before creating the layer.

Can I write or modify SQL directly?

Yes, the SQL editor allows you to write or modify SQL queries directly if you prefer not to use the AI prompt feature.

Last updated

Was this helpful?