ST_Within(A, B) returns true if the following criteria are met:
- No point in A lies outside of B.
- At least one point in the interior of A lies in the interior of B.
ST_CoveredBy, except that the criteria are more exacting, and therefore some pairs of shapes will be rejected by this function that would be accepted by ST_CoveredBy.
ST_Within works on the following spatial data types:
“ will attempt to use any available spatial index to speed up its operation. Use the prefixed variant
_ if you do not want any spatial indexes to be used.This function is the inverse of
ST_Contains.Examples
The screenshots in these examples were generated using geojson.io, but they are designed to showcase the shapes, not the map. Representing
GEOMETRY data in GeoJSON can lead to unexpected results if using geometries with SRIDs other than 4326 (as shown below).True
In this example, “ returnstrue because:
- No point in Polygon A lies outside of Polygon B.
- At least one point in the interior of Polygon A lies in the interior of Polygon B.

False
In this example, “ returnsfalse because:
- All points in Polygon A lie outside of Polygon B.

See also
- Export Spatial Data
- Spatial tutorial
- Spatial and GIS Glossary of Terms
- Spatial indexes
- Spatial functions
ST_CoversST_CoveredByST_ContainsST_IntersectsST_CoveredByST_CoversST_DisjointST_EqualsST_OverlapsST_TouchesST_ConvexHullST_Union- Migrate from Shapefiles
- Migrate from GeoJSON
- Migrate from GeoPackage
- Migrate from OpenStreetMap
- Introducing Distributed Spatial Data in CockroachDB (blog post)
- Using GeoServer with CockroachDB

