Skip to content

ST_UNION

Combines two GEOGRAPHY objects into a single shape without overlaps. This function is commonly used to merge complex GEOGRAPHY shapes into a simpler one.

There are two variants of this function:

  • Two-input variant: Returns a GEOGRAPHY object representing the union of two input geographies. If either input is NULL, the result is NULL.
  • Aggregate variant: Returns a GEOGRAPHY object representing the union of a rowset of geographies. NULL values are ignored in this variant.

This function is symmetric, meaning:

  • ST_UNION(A, B) = ST_UNION(B, A)

See also:

Syntax

sql
-- Two-input variant:
ST_UNION(<a>, <b>)

-- Aggregate variant:
ST_UNION(<geography_column>)

Arguments

  • <a> and <b>: The two GEOGRAPHY objects to be merged

Returns

Returns a GEOGRAPHY object representing the union of the input geographies.

The output type can be:

  • A GEOGRAPHY object
  • A MultiGeometry
  • A GeometryCollection

Example

sql
CREATE VIEW v AS
              SELECT 'POINT(1 1)' geo
    UNION ALL SELECT 'LINESTRING(1 1,2 2)'
    UNION ALL SELECT 'POINT(3 3)';

SELECT ST_AsText(ST_Union(geo::GEOGRAPHY)) from v;
-- 'GEOMETRYCOLLECTION (LINESTRING (1 1, 2 2), POINT (3 3))'
-- Note: POINT(1 1) was merged into LINESTRING(1 1,2 2)