ST_DIFFERENCE()
All functions > GEOSPATIAL > ST_DIFFERENCE()
Returns the geometry/geography representing the point set difference of A minus B.
Signatures
Difference
Returns: A minus B
ST_DIFFERENCE(geo1: T, geo2: T) → GEOMETRY or GEOGRAPHY sql
| Parameter | Type | Required | Description |
|---|---|---|---|
geo1 | T | Yes | Geometry or geography to subtract from |
geo2 | T | Yes | Geometry or geography to subtract |
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:
- Returns the part of A that does not intersect B
- If A is fully within B, returns an empty geometry
- Order matters: ST_DIFFERENCE(A, B) is not the same as ST_DIFFERENCE(B, A)
- ST_ASTEXT output may use either winding direction for rings; consumers that require strict OGC exterior orientation should normalize before exchange
- Both inputs must be the same type family
- Returns NULL if either input is NULL
Examples
FeatureQL
SELECT
f1 := ST_ASTEXT(ST_DIFFERENCE(ST_GEOMFROMTEXT('POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0))'), ST_GEOMFROMTEXT('POLYGON ((1 1, 4 1, 4 4, 1 4, 1 1))'))) -- L-shaped remainder after subtracting overlapping square (WKT vertex order may vary)
;Result
| f1 VARCHAR |
|---|
| POLYGON ((3 0, 0 0, 0 3, 1 3, 1 1, 3 1, 3 0)) |
On this page