PERMUTATIONS()

All functions > ARRAY > PERMUTATIONS()

Returns all ordered 2-element selections from the input array as an array of 2-element arrays.

Signatures

Returns: All ordered pairs of distinct positions

PERMUTATIONS(array: ARRAY<T>) → ARRAY<ARRAY<T>>
sql
ParameterTypeRequiredDescription
arrayARRAY<T>YesInput array (scalar or ROW elements)

Notes

  • Always selects ordered pairs of two elements (fixed arity)
  • Supports scalar arrays, arrays of rows (ARRAY<ROW(...)>), and nested arrays (ARRAY<ARRAY<T>>)
  • Uses distinct positions: the same value at two indices yields two ordered pairs
  • Returns an empty array when fewer than two elements are available

See also

Examples

FeatureQL
SELECT
    -- All ordered pairs
    f1 := PERMUTATIONS(ARRAY(1, 2, 3)),
    -- Two-element array
    f2 := PERMUTATIONS(ARRAY('a', 'b')),
    -- Empty input array
    f3 := PERMUTATIONS(ARRAY()::BIGINT[]),
    -- Ordered pairs of rows
    f4 := PERMUTATIONS(ZIP(ARRAY(1, 2) AS x, ARRAY('a', 'b') AS y))
;
Result
f1 ARRAYf2 ARRAYf3 ARRAYf4 ARRAY
[[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]][[a, b], [b, a]][][[{x: 1, y: a}, {x: 2, y: b}], [{x: 2, y: b}, {x: 1, y: a}]]

Last update at: 2026/06/20 10:08:10