Skip to content

SQL - PostgreSQL - Reverse an Array

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

Explore this snippet here.

Description

There is no built-in function for reversing an array, but it is possible to construct one using the generate_subscripts function:

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

select
  array(
    select nums[i]
    from generate_subscripts(nums, 1) as indices(i)
    order by i desc
  ) as reversed
from data

References


Backlinks:

list from [[SQL - PostgreSQL - Reverse an Array]] AND -"Changelog"