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

# ST_Within

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

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 <InternalLink path="st_coveredby">`ST_CoveredBy`</InternalLink>, 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:

* <InternalLink path="architecture/glossary#geometry">`GEOMETRY`</InternalLink>

<Note>
  `ST_Within` will attempt to use any available <InternalLink path="spatial-indexes">spatial index</InternalLink> to speed up its operation.
  Use the prefixed variant `_ST_Within` if you do not want any spatial indexes to be used.
</Note>

<Note>
  This function is the inverse of <InternalLink path="st_contains">`ST_Contains`</InternalLink>.
</Note>

## 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 <InternalLink path="architecture/glossary#srid">SRIDs</InternalLink> other than 4326 (as shown below).
</Note>

### True

In this example, `ST_Within` 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))'));
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   st_within
---------------
     true

(1 row)
```

<img src="https://mintcdn.com/cockroachlabs/LiqnOtencKgegCU0/images/v25.2/geospatial/st_within_true.png?fit=max&auto=format&n=LiqnOtencKgegCU0&q=85&s=f6ccce9ad52a69da3167d90e99a97d79" alt="ST_Within - true" width="1280" height="988" data-path="images/v25.2/geospatial/st_within_true.png" />

### False

In this example, `ST_Within` 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))'));
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   st_within
---------------
     false
(1 row)
```

<img src="https://mintcdn.com/cockroachlabs/LiqnOtencKgegCU0/images/v25.2/geospatial/st_within_false.png?fit=max&auto=format&n=LiqnOtencKgegCU0&q=85&s=c4ccb0f1aecc33f9d7da7c509adc6199" alt="ST_Within - false" width="1280" height="988" data-path="images/v25.2/geospatial/st_within_false.png" />

## See also

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