ST_DWITHIN()

All functions > GEOSPATIAL > ST_DWITHIN()

Returns TRUE if the distance between two geometries or geographies is less than or equal to a threshold.

Signatures

Distance within

Returns: TRUE if distance between A and B <= distance

ST_DWITHIN(geo1: T, geo2: T, distance: DOUBLE) → BOOLEAN
sql
ParameterTypeRequiredDescription
geo1TYesFirst geometry or geography
geo2TYesSecond geometry or geography
distanceDOUBLEYesMaximum distance threshold

With:

  • T : Custom types: GEOM_POINT | GEOM_LINESTRING | GEOM_POLYGON | GEOM_MULTIPOINT | GEOM_MULTILINESTRING | GEOM_MULTIPOLYGON | GEOM_MULTIANY | GEOG_POINT | GEOG_LINESTRING | GEOG_POLYGON | GEOG_MULTIPOINT | GEOG_MULTILINESTRING | GEOG_MULTIPOLYGON | GEOG_MULTIANY

Signature notes:

  • Equivalent to ST_DISTANCE(a, b) <= distance, but potentially optimized with spatial indexing
  • On GEOMETRY: distance is in SRID coordinate units
  • On GEOGRAPHY: distance is in meters
  • On Trino: transpiled to ST_Distance() <= distance (no native ST_DWithin)
  • Both geometry inputs must be the same type family
  • Returns NULL if any input is NULL

Examples

Geometry

FeatureQL
SELECT
    f1 := ST_DWITHIN(ST_GEOMPOINT(0.0, 0.0), ST_GEOMPOINT(3.0, 4.0), 5.0), -- Distance is exactly 5.0, within threshold
    f2 := ST_DWITHIN(ST_GEOMPOINT(0.0, 0.0), ST_GEOMPOINT(3.0, 4.0), 4.0) -- Distance is 5.0, exceeds threshold of 4.0
;
Result
f1 BOOLEANf2 BOOLEAN
truefalse

Geography (distance in meters)

FeatureQL
SELECT
    f1 := ST_DWITHIN(ST_GEOGPOINT(-122.4194, 37.7749), ST_GEOGPOINT(-122.4089, 37.7837), 2000.0) -- Two points in San Francisco within 2 km
;
Result
f1 BOOLEAN
true

Last update at: 2026/05/26 17:22:09