Skip to main content

ST_SymDifference

Returns the symmetric difference of two geometries - the portions that are in either geometry but not in both.

Syntax

ST_SymDifference(geometry_a, geometry_b)

Parameters

ParameterTypeDescription
geometry_aGeometryFirst geometry
geometry_bGeometrySecond geometry

Returns

TypeDescription
Binary (WKB)Symmetric difference as WKB with geoarrow.wkb metadata

Examples

XOR of Overlapping Polygons

SELECT ST_SymDifference(
ST_GeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))'),
ST_GeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))')
);
-- Returns: two separate polygons (non-overlapping parts)

Find Non-Overlapping Areas

SELECT ST_SymDifference(old_boundary.geom, new_boundary.geom) as changed_area
FROM boundaries old_boundary, boundaries new_boundary
WHERE old_boundary.year = 2020 AND new_boundary.year = 2024;

Compare Versions

SELECT
ST_Area(ST_SymDifference(v1.geom, v2.geom)) as difference_area,
ST_Area(ST_Intersection(v1.geom, v2.geom)) as unchanged_area
FROM parcels_v1 v1, parcels_v2 v2
WHERE v1.id = v2.id;

Symmetric Result

SELECT
ST_Equals(
ST_SymDifference(a.geom, b.geom),
ST_SymDifference(b.geom, a.geom)
) as is_symmetric
FROM regions a, regions b;
-- Always returns true

Notes

  • Equivalent to (A - B) UNION (B - A)
  • Operation is commutative: ST_SymDifference(A, B) = ST_SymDifference(B, A)
  • Returns empty geometry if A equals B
  • Useful for change detection between versions

See Also