Spatial Analysis in 2025: Key Trends Report
| Download Now
CARTO
Platform
Overview
Visualization
Analytics
App Development
Data Enrichment
AI Agents
Enterprise & Security
Pricing
CARTO for
Google Cloud
AWS
Azure
Snowflake
Databricks
Oracle
Solutions
By Industry
By Use Case
By Role
Telecoms
Icon/indus/Insurance-menu
Icon/indus/Insurance-menu-mobile
Insurance
Logistics
Real Estate
Financial Services
Retail
Marketing & Advertising
Mobility
All industries
Network Deployment
Catastrophe Modeling
Fraud Detection
Market Analysis
Environmental Management
Site Selection
Geomarketing
Data Monetization
IoT Analytics
Supply Chain Optimization
Healthcare Analytics
Territory Planning
All use cases
Data Analyst
Data Scientist
GIS Professional
Developer
Customers
Resources
Learn
Documentation
CARTO Academy
Blog
Reports
Glossary
Connect
Events
Partners
Webinars
Log inRequest a demo
Try for free
2050-01-01
1990-01-01
"ES", "GB"

This content applies to a previous version of CARTO

In October 2021 we released a new version of our platform. You can find the latest documentation at docs.carto.com

Back to Help Center

Tips for Advanced Analysis

Learn how to work with bbox and joins in SQL.

Get All Records at a Point

One of the most common geospatial queries web-services experience is one to find all records that occur at a given location. CARTO makes this very simple, with just a few parameters and the SQL API, you can quickly build the results of these queries into your application.

Records at a Point

SELECT
  cartodb_id,
  the_geom,
  the_geom_webmercator
FROM
  {table_name}
WHERE
  ST_Intersects( the_geom, cdb_latlng(40.7248057566452,-73.9967118782795))

This retrieves all records intersecting a point created dynamically. The query selects three columns, performing analysis on the_geom while including the_geom_webmercator in results for map visualization.

Query Records in a Bounding Box

Querying records within a bounding box is a common application requirement, easily accomplished using SQL.

Bounding Box

SELECT
  *
FROM
  {table_name}
WHERE
  the_geom && ST_SetSRID(ST_MakeBox2D(ST_Point(-73.9980, 40.726), ST_Point(-73.995, 40.723)), 4326)

The && operator uses geometry bounding boxes, unlike ST_Intersects which evaluates full shapes. This approach significantly improves query performance when bounding box comparison suffices.

Join Data from Two Tables

After mastering basic SQL, combining data from multiple tables through a single query becomes essential.

Table Joins

SELECT
  {table_name_1}.iso,
  {table_name_1}.value,
  {table_name_2}.the_geom
FROM
  {table_name_1},
  {table_name_2}
WHERE
  {table_name_1}.iso = {table_name_2}.iso

This example returns columns iso and value from the first table with geometry from the second, matching rows based on shared iso codes – a frequent pattern when merging values with geometric data.

Sort Records by Distance to a Point

A compelling use case: identifying the 10 nearest database entries to a specific location, particularly valuable for mobile and location-aware applications.

Distance Sort

SELECT
  ST_X(ST_Centroid(the_geom)) as longitude,
  ST_Y(ST_Centroid(the_geom)) as latitude,
  description,
  ST_Distance(the_geom::geography, ST_PointFromText('POINT(-73.999548 40.71954)', 4326)::geography) AS distance
FROM
  {table_name}
ORDER BY
  the_geom <-> ST_PointFromText('POINT(-73.999548 40.71954)', 4326)
LIMIT 10

The statement extracts longitude and latitude using ST_X and ST_Y functions, calculates distance to a dynamically defined point, and orders results by ascending distance with a limit of 10 closest points.

The <-> operator functions as a PostGIS bounding box index, improving performance when paired with ORDER BY and LIMIT clauses.

Common Table Expression

Common Table Expressions enhance SQL readability but constrain the query planner by forcing execution order.

It is strongly recommended not to use CTE statements in CARTO queries, particularly in maps where the Maps API wraps queries to optimize tile creation. When the query planner cannot access complete statements, it cannot optimize effectively.

CARTO
carto-logo
  • Platform

  • Overview

  • Visualization

  • Analytics

  • App Development

  • Data Enrichment

  • Security & Governance

  • Gen AI

  • Pricing

  • Solutions

  • Data Analyst

  • Developer

  • Data Scientist

  • GIS Professional

  • By Industry

  • By Use Case

  • Resources

  • Customer Stories

  • Blog

  • Glossary

  • Documentation

  • Academy

  • Reports

  • Events

  • Webinars

  • Partners

  • Company

  • About us

  • Newsroom

  • Careers

  • Brand

  • Grants

Unlock the power of spatial analysis.

youtube logofacebook logotwitter logolinkedin logo

© CARTO 2024

Terms

Privacy Notice

Whistleblower Form