ST_Union function

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

Read syntax diagramSkip visual syntax diagram ST_Union ( geometry1 , geometry2 )

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.

CREATE TABLE sample_geoms (id INTEGER, geometry, ST_Geometry)

INSERT INTO sample_geoms
  VALUES (1, ST_Geometry( 'polygon 
         ((10 10, 10 20, 20 20, 20 10, 10 10) )', 0))

INSERT INTO sample_geoms
  VALUES (2, ST_Geometry( 'polygon 
         ((30 30, 30 50, 50 50, 50 30, 30 30) )', 0))

INSERT INTO sample_geoms
  VALUES (3, ST_Geometry( 'polygon 
         ((40 40, 40 60, 60 60, 60 40, 40 40) )', 0))

INSERT INTO sample_geoms
  VALUES (4, ST_Geometry('linestring (70 70, 80 80)', 0))

INSERT INTO sample_geoms
  VALUES (5, ST_Geometry('linestring (80 80, 100 70)', 0))

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
Results:

ID1  ID2  UNION
---- ---- -------------------------------------------------------
   1    2 MULTIPOLYGON (((10 10, 20 10, 20 20, 10 20, 10 10)),
                          ((30 30, 50 30, 50 50, 30 50, 30 30)))

Example 3
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
Results:

ID1  ID2  UNION
---- ---- ----------------------------------------------------
   2    3 POLYGON ((30 30, 50 30, 50 40, 60 40, 60 60, 40 60, 
                      40 50, 30 50, 30 30))
  
Example 4
Find the union of two linestrings.

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
Results:

ID1  ID2  UNION
---- ---- ---------------------------------------------------------------
   4    5 MULTILINESTRING ((70 70, 80 80),(80 80, 100 70))