> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ST_Within

Given two shapes *A* and *B*, the predicate function `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*.

In other words, the exterior of shape *B* must not include any point in *A*, and one or more points of *A*'s interior must lie to the interior of *B*.

This behavior is similar to [`ST_CoveredBy`](https://docs.cockroachlabs.com/docs/v24.2/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:

* [`GEOMETRY`](https://docs.cockroachlabs.com/docs/v24.2/architecture/glossary#geometry)

> [!NOTE]
>
> \`\` will attempt to use any available [spatial index](https://docs.cockroachlabs.com/docs/v24.2/spatial-indexes) to speed up its operation.  Use the prefixed variant `_` if you do not want any spatial indexes to be used.

> [!NOTE]
>
> This function is the inverse of [`ST_Contains`](https://docs.cockroachlabs.com/docs/v24.2/st_contains).

## Examples

> [!NOTE]
>
> The screenshots in these examples were generated using [geojson.io](http://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](https://docs.cockroachlabs.com/docs/v24.2/architecture/glossary#srid) other than 4326 (as shown below).

### True

In this example, \`\` returns `true` 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*.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT ST_Within(st_geomfromtext('SRID=4326;POLYGON((-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))'), st_geomfromtext('SRID=4326;POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902))'));
```

```
   st_within
---------------
     true

(1 row)
```

![ST\_Within - true](https://mintcdn.com/cockroachlabs/bBD0UEapbn6fk8Rb/images/common/geospatial/st_within_true.png?fit=max\&auto=format\&n=bBD0UEapbn6fk8Rb\&q=85\&s=e263ee80a31d2823916060115469116d)

### False

In this example, \`\` returns `false` because:

* All points in Polygon *A* lie outside of Polygon *B*.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SELECT ST_Within(st_geomfromtext('SRID=4326;POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902), (-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))'), st_geomfromtext('SRID=4326;POLYGON((-87.356934 41.595161, -84.512016 39.103119, -86.529167 39.162222, -87.356934 41.595161))'));
```

```
   st_within
---------------
     false
(1 row)
```

![ST\_Within - false](https://mintcdn.com/cockroachlabs/bBD0UEapbn6fk8Rb/images/common/geospatial/st_within_false.png?fit=max\&auto=format\&n=bBD0UEapbn6fk8Rb\&q=85\&s=f1813b31054761204c56afd8c52e68bb)

## See also

* [Export Spatial Data](https://docs.cockroachlabs.com/docs/v24.2/export-spatial-data)
* [Spatial tutorial](https://docs.cockroachlabs.com/docs/v24.2/spatial-tutorial)
* [Spatial and GIS Glossary of Terms](https://docs.cockroachlabs.com/docs/v24.2/architecture/glossary)
* [Spatial indexes](https://docs.cockroachlabs.com/docs/v24.2/spatial-indexes)
* [Spatial functions](https://docs.cockroachlabs.com/docs/v24.2/functions-and-operators#spatial-functions)
* [`ST_Covers`](https://docs.cockroachlabs.com/docs/v24.2/st_covers)
* [`ST_CoveredBy`](https://docs.cockroachlabs.com/docs/v24.2/st_coveredby)
* [`ST_Contains`](https://docs.cockroachlabs.com/docs/v24.2/st_contains)
* [`ST_Intersects`](https://docs.cockroachlabs.com/docs/v24.2/st_intersects)
* [`ST_CoveredBy`](https://docs.cockroachlabs.com/docs/v24.2/st_coveredby)
* [`ST_Covers`](https://docs.cockroachlabs.com/docs/v24.2/st_covers)
* [`ST_Disjoint`](https://docs.cockroachlabs.com/docs/v24.2/st_disjoint)
* [`ST_Equals`](https://docs.cockroachlabs.com/docs/v24.2/st_equals)
* [`ST_Overlaps`](https://docs.cockroachlabs.com/docs/v24.2/st_overlaps)
* [`ST_Touches`](https://docs.cockroachlabs.com/docs/v24.2/st_touches)
* [`ST_ConvexHull`](https://docs.cockroachlabs.com/docs/v24.2/st_convexhull)
* [`ST_Union`](https://docs.cockroachlabs.com/docs/v24.2/st_union)
* [Migrate from Shapefiles](https://docs.cockroachlabs.com/docs/v24.2/migrate-from-shapefiles)
* [Migrate from GeoJSON](https://docs.cockroachlabs.com/docs/v24.2/migrate-from-geojson)
* [Migrate from GeoPackage](https://docs.cockroachlabs.com/docs/v24.2/migrate-from-geopackage)
* [Migrate from OpenStreetMap](https://docs.cockroachlabs.com/docs/v24.2/migrate-from-openstreetmap)
* [Introducing Distributed Spatial Data in CockroachDB](https://www.cockroachlabs.com/blog/spatial-data/) (blog post)
* [Using GeoServer with CockroachDB](https://docs.cockroachlabs.com/docs/v24.2/geoserver)
