Skip to content

SQL - PostgreSQL - Get the Last Element of an Array

Source: sql-snippets/get-last-array-element.md at main · count/sql-snippets (github.com)

Explore this snippet here.

Description

Use the array_length function to access the final element of an array. This function takes two arguments, an array and a dimension index. For common one-dimensional arrays, the dimension index is always 1.

with data as (select array[1,2,3,4] as nums)

select nums[array_length(nums, 1)] from data

Backlinks:

list from [[SQL - PostgreSQL - Get the Last Element of an Array]] AND -"Changelog"