Spatial Joins

~15 min read

Spatial Joins

Spatial joins combine data from two tables based on their geographic relationship.

Point in Polygon

Find which country each city belongs to (assuming you have a countries table):

sql
SELECT c.name AS city, co.name AS country
FROM cities c
JOIN countries co ON ST_Within(c.geom, co.geom);

Intersection

Find all roads that cross a flood zone:

sql
SELECT r.name AS road
FROM roads r
JOIN flood_zones fz ON ST_Intersects(r.geom, fz.geom);

Key Spatial Predicates

| Function | Meaning |
|----------|---------|
| ST_Within | A is completely inside B |
| ST_Intersects | A and B share any space |
| ST_Contains | B is completely inside A |
| ST_Crosses | A crosses through B |

Spatial joins are one of the most powerful tools in your GIS SQL toolkit.

← Previous🎉 Last lesson in the course