All Products
Search
Document Center

PolarDB:ST_Within

Last Updated:Sep 01, 2026

Returns true if geometry A is completely inside geometry B: no point of A lies outside B, and at least one interior point of A lies in the interior of B.

Syntax

boolean ST_Within(geometry A, geometry B)

Parameters

ParameterDescription
AThe geometry to test.
BThe geometry to test against.

Description

ST_Within(A, B) returns true when both of the following conditions are met:

  • No point of A lies outside B.

  • At least one interior point of A lies in the interior of B.

Relationship to other functions:

  • If both ST_Within(A, B) and ST_Within(B, A) return true, the two geometry objects are spatially equal.

Constraints:

  • Both geometry objects must use the same projection and have the same spatial reference identifier (SRID).

  • GeometryCollection objects are not supported.

  • Invalid geometry objects produce an incorrect result.

Index behavior:

ST_Within generates a bounding box and uses any available spatial indexes on the geometry objects. To run the containment test without index acceleration, use _ST_Within.

Examples

Example 1: Polygon inside polygon

The smaller polygon (1,1)–(2,2) lies entirely inside the larger polygon (0,0)–(3,3), so ST_Within returns true.

SELECT ST_Within(
  'POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))'::geometry,
  'POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))'::geometry
);

 st_within
-----------
 t
(1 row)