Introduction
Functions returning geometries
Collections
11. ST_GeometryN
Summary

Instruction

Nicely done! There are a couple of small functions that can be used to work with collections. We will discuss a few that may prove useful.

The first function is ST_GeometryN(geometry, n). It returns the nth geometry in the collection, for instance:

SELECT
  ST_GeometryN(geo_data,1) 
FROM sf_chains;

The query above will return the first element of each collection in sf_chains.

Note that elements in the collection are numbered starting from 1.

Exercise

Mark has taken a particular liking to one of the Aquamarine Prairie Hotel & Spa (id = 2). For the row with id = 2 in the sf_chains table, return the third item in the internal PostGIS format, its text representation and the description column.

Stuck? Here's a hint!

Use ST_GeometryN(geo_data,3). Then, surround this call with ST_AsText.

Console

Code editor

Result

TableConsoleMap