The ST_Union function takes two geometries as input parameters and returns the geometry
that is the union of the specified geometries. The resulting geometry is represented in the spatial
reference system of the first geometry.

Both geometries must be of the same dimension. If any of the two specified geometries is null,
null is returned.

If the
second geometry is not represented in the same spatial reference system
as the first geometry and uses the same underlying datum, it will
be converted to the other spatial reference system.

The resulting geometry is represented in the most appropriate spatial type. If it can be
represented as a point, linestring, or polygon, then one of those types is used. Otherwise, the
multipoint, multilinestring, or multipolygon type is used.

Syntax

Parameters

geometry1

A value of type ST_Geometry or one of its subtypes that is combined with geometry2.

geometry2

A value of type ST_Geometry or one of its subtypes that is combined with geometry1.

Return type

ST_Geometry

Examples

Example 1

The following SQL statements create and populate the SAMPLE_GEOMS table.

In
the following examples, the results have been reformatted for readability. Your results will vary
according to your display.

Example 2

This example finds the union of two disjoint polygons.

SELECT a.id as id1, b.id as id2, CAST ( ST_AsText( ST_Union( a.geometry, b.geometry) )
AS VARCHAR (180) ) UNION
FROM sample_geoms a, sample_geoms b
WHERE a.id = 1 AND b.id = 2

This example finds the union of two intersecting polygons.

SELECT a.id as id1, b.id as id2, CAST ( ST_AsText( ST_Union(a.geometry, b.geometry))
AS VARCHAR (180)) UNION
FROM sample_geoms a, sample_geoms b
WHERE a.id = 2 AND b.id = 3

SELECT a.id as id1, b.id as id2, CAST ( ST_AsText( ST_Union( a.geometry, b.geometry) )
AS VARCHAR (180) ) UNION
FROM sample_geoms a, sample_geoms b
WHERE a.id = 4 AND b.id = 5