Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Functions returning geometries
8. ST_Union
Collections
Summary

Instruction

Good job! The last function we'll briefly discuss in this section is ST_Union. It returns a new geometry a new geometry that is the sum (or union-hence the name) of input geometries.

ST_Union

Interestingly, there are two versions of the function:

  • Non-aggregate: provide two or more input geometries in the brackets to combine them: ST_Union(geometry1, geometry2). The result will be a new geometry created from all input geometries. This version works in a very similar way to ST_Intersection, which we discussed in the previous exercise.
  • Aggregate: in this version of ST_Union, we only need a single argument: a column name. ST_Union will then work similarly to aggregate functions such as SUM(...). It will create a new geometry by adding up ALL geometries in the given column that were returned by the query. This can be very convenient. Take a look at the example:

    SELECT 
      ST_Union(boundaries)
    FROM sf_planning_districts;
    This query will return a single geometry representing the union of all planning districts of San Francisco.

Exercise

Return a single geometry that represents all recreational districts of San Francisco.

Stuck? Here's a hint!

Simply use the aggregate version of ST_Union. In the WHERE clause, specify the district type ('recreational').