View a markdown version of this page

Range and unnest functions - Amazon Athena

Range and unnest functions

Use these functions to generate a sequence of numbers or timestamps, and to expand a list into rows, with one row for each element or position. Some of these functions also work as table functions in the FROM clause.

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

generate_series function

In an expression, generate_series returns a LIST of the values from start through stop, in increments of step. In the FROM clause, it is a table function that returns the same values as rows, in one column named generate_series. Both forms include stop when the series reaches it exactly. To leave out stop, use the range function.

With only stop, the values start at 0. The default step is 1. A negative step counts down from a start that is greater than stop. When step moves away from stop, as in generate_series(5, 1), the list is empty and the table has no rows. If any argument is NULL, the expression form returns NULL and the table form returns no rows.

For TIMESTAMP and TIMESTAMP WITH TIME ZONE values, step is an INTERVAL and has no default. DATE arguments are converted to TIMESTAMP, so the values are timestamps. Cast them to DATE when you need dates.

Syntax

generate_series(stop) generate_series(start, stop [, step ]) FROM generate_series(stop) [ AS alias(column_name) ] FROM generate_series(start, stop [, step ]) [ AS alias(column_name) ]

Arguments

The generate_series function takes the following arguments:

start

The first value, as a BIGINT, TIMESTAMP, or TIMESTAMP WITH TIME ZONE. When you pass only stop, start is 0.

stop

The last value that the series can include, of the same type as start.

step

The increment between values. For numbers, a BIGINT with a default of 1. For timestamps, an INTERVAL, such as INTERVAL 1 DAY, which you must pass.

alias

A name for the table that generate_series returns, in the table form only. You need it when you name the output column. This argument is optional.

column_name

A name for the output column, in the table form only. Without it, the column is named generate_series. This argument is optional.

Return type

In an expression, a LIST of the type of start: BIGINT[], TIMESTAMP[], or TIMESTAMP WITH TIME ZONE[]. In the FROM clause, a table with one column of that element type, such as BIGINT or TIMESTAMP.

Examples

The following SQL query returns a list of the values from 2 through 5 in steps of 3. The list includes 5 because 2 plus one step reaches it exactly.

SELECT generate_series(2, 5, 3) AS result

The query returns the following result.

result

[2, 5]

The following SQL query compares generate_series with range, and shows the one-argument form, which starts at 0. generate_series includes the stop value, and range does not.

SELECT generate_series(1, 5) AS inclusive_stop, range(1, 5) AS exclusive_stop, generate_series(3) AS from_zero

The query returns the following result.

inclusive_stop exclusive_stop from_zero

[1, 2, 3, 4, 5]

[1, 2, 3, 4]

[0, 1, 2, 3]

The following SQL query uses the table form to return one row for each day from October 1 through October 3, 2026. The table alias names the output column ts.

SELECT ts FROM generate_series( TIMESTAMP '2026-10-01', TIMESTAMP '2026-10-03', INTERVAL 1 DAY ) AS s(ts) ORDER BY ts

The query returns the following results.

ts

2026-10-01 00:00:00

2026-10-02 00:00:00

2026-10-03 00:00:00

The following SQL query counts the orders for each day from October 1 through October 5, 2026, including days that have no orders. The table form produces one row for each day, and the LEFT JOIN keeps the days that have no matching rows in sales_db.sales_parquet.

SELECT CAST(d.day_ts AS DATE) AS order_day, count(s.order_id) AS orders FROM generate_series( TIMESTAMP '2026-10-01', TIMESTAMP '2026-10-05', INTERVAL 1 DAY ) AS d(day_ts) LEFT JOIN sales_db.sales_parquet AS s ON s.order_date = CAST( d.day_ts AS DATE ) GROUP BY CAST(d.day_ts AS DATE) ORDER BY order_day

The query returns the following results.

order_day orders

2026-10-01

2

2026-10-02

1

2026-10-03

2

2026-10-04

1

2026-10-05

0

Considerations

With a month interval as step, each value is computed from the one before it. As a result, a series that starts on the last day of a month can drift to an earlier day. For example, from 2026-01-31 with INTERVAL 1 MONTH, the values are 2026-01-31, 2026-02-28, 2026-03-28, and 2026-04-28.

A step of 0 makes the table form fail. In an expression, it returns an empty list.

Differences from Athena engine version 3

In Athena engine version 3, you build a list of values from a start value through a stop value with sequence(start, stop [, step ]). DuckDB has no sequence function. Use generate_series, which also includes stop, rather than range, which leaves it out. For DATE arguments, generate_series returns timestamps, so to get a list of dates, cast each element, for example list_transform(generate_series(start, stop, INTERVAL 1 DAY), lambda x: CAST(x AS DATE)).

generate_subscripts function

Returns the numbers from 1 through the number of elements in list, one number per output row. Like unnest, it is a set-returning function: in the SELECT list, it produces one row for each position. Use the positions to number the elements of a list, or as indexes into the list.

dimension selects the level of nesting to count. For a LIST, it must be 1. For a nested fixed-size ARRAY, such as INTEGER[3][2], you can also pass 2 to count the positions in the inner arrays. If list is empty or NULL, the function returns no rows.

Syntax

generate_subscripts(list [, dimension ])

Arguments

The generate_subscripts function takes the following arguments:

list

The list whose element positions you want, as a LIST or ARRAY value.

dimension

The level of nesting to count, as an INTEGER value, where 1 is the top level. The default is 1. This argument is optional.

Return type

BIGINT, one row for each position.

Examples

The following SQL query returns the positions of the three elements of a list, as three rows.

SELECT generate_subscripts([4, 5, 6], 1) AS result ORDER BY result

The query returns the following results.

result

1

2

3

The following SQL query pairs each element of a list with its position, by calling unnest on the same list.

SELECT generate_subscripts(['a', 'b', 'c'], 1) AS idx, unnest(['a', 'b', 'c']) AS letter ORDER BY idx

The query returns the following results.

idx letter

1

a

2

b

3

c

The following SQL query returns one row for each item of each order, with the position of the item in its list. The query also uses the position as an index to read the item.

SELECT order_id, i AS position, items[i] AS item FROM ( SELECT order_id, items, generate_subscripts(items, 1) AS i FROM (VALUES (1, ['pen', 'ink']), (2, ['pad']) ) AS t(order_id, items) ) ORDER BY order_id, position

The query returns the following results.

order_id position item

1

1

pen

1

2

ink

2

1

pad

The following SQL query shows that an empty list produces no rows, so the count is 0.

SELECT count(*) AS row_count FROM (SELECT generate_subscripts([]::INTEGER[], 1) AS i)

The query returns the following result.

row_count

0

Considerations

For a LIST, a dimension other than 1 makes the query fail.

generate_subscripts is not a table function, so you cannot use it in the FROM clause. Call it in the SELECT list.

range function

Returns the values that begin at start and change by step each time, for as long as they are less than stop, or greater than stop when step is negative. The stop value itself is never included. When you leave out start, it is 0, and when you leave out step, it is 1. If no value meets the condition, for example when start is greater than stop and step is positive, the result has no values.

In an expression, range returns a LIST, and a NULL argument makes the result NULL. In the FROM clause, range is a table function that returns one row for each value, in a column named range. To name the column, use an alias, as in FROM range(3) AS r(n).

To build a range of timestamps, pass TIMESTAMP or TIMESTAMP WITH TIME ZONE values and an INTERVAL step. To include stop in the result, use the generate_series function, which takes the same arguments.

Syntax

range(stop) range(start, stop [, step ]) FROM range(stop) [ AS alias(column) ] FROM range(start, stop [, step ]) [ AS alias(column) ]

Arguments

The range function takes the following arguments:

start

The first value: a BIGINT, or a TIMESTAMP or TIMESTAMP WITH TIME ZONE value. A DATE value is converted to TIMESTAMP. For numbers, this argument is optional, and the default is 0. For timestamps, start, stop, and step are all required.

stop

The value at which the range ends, of the same type as start. stop is not included in the result.

step

The amount to add to each value to get the next one: a BIGINT for numbers, or an INTERVAL for timestamps. For numbers, this argument is optional, the default is 1, and a negative value counts down.

Return type

In an expression, BIGINT[] for numbers, TIMESTAMP[] for TIMESTAMP or DATE arguments, and TIMESTAMP WITH TIME ZONE[] for TIMESTAMP WITH TIME ZONE arguments. In the FROM clause, a table with one column, range, of type BIGINT, TIMESTAMP, or TIMESTAMP WITH TIME ZONE.

Examples

The following SQL query calls range with one, two, and three arguments. None of the results includes the stop value.

SELECT range(5) AS one_arg, range(2, 5) AS two_args, range(2, 5, 3) AS three_args

The query returns the following result.

one_arg two_args three_args

[0, 1, 2, 3, 4]

[2, 3, 4]

[2]

The following SQL query counts down with a negative step, and shows that a start value greater than the stop value returns an empty list when the step is positive.

SELECT range(10, 0, -3) AS counting_down, range(5, 2) AS no_values

The query returns the following result.

counting_down no_values

[10, 7, 4, 1]

[]

The following SQL query uses range as a table function in the FROM clause, and names its column n.

SELECT n FROM range(2, 11, 4) AS r(n) ORDER BY n

The query returns the following results.

n

2

6

10

The following SQL query builds a range of days from DATE values. The elements are TIMESTAMP values, and the stop date is not included.

SELECT range( DATE '2024-01-01', DATE '2024-01-04', INTERVAL 1 DAY ) AS result

The query returns the following result.

result

['2024-01-01 00:00:00', '2024-01-02 00:00:00', '2024-01-03 00:00:00']

Considerations

When you pass DATE values, the result holds TIMESTAMP values. To get DATE values, cast each one, as in list_transform(range(start, stop, INTERVAL 1 DAY), lambda d: CAST(d AS DATE)).

Differences from Athena engine version 3

Athena engine version 3 builds an array of numbers with sequence(start, stop), which includes stop. range(start, stop) leaves out stop. To get the Athena engine version 3 result, use generate_series(start, stop).

In Athena engine version 3, sequence(start, stop) counts down by 1 when start is greater than stop. In DuckDB, the default step is always 1, so range(5, 1) and generate_series(5, 1) both return an empty list. To count down, pass a negative step, as in range(5, 0, -1) or generate_series(5, 1, -1).

unnest function

Returns one row for each element of list. In the FROM clause, unnest(list) is a table function that returns those rows in a column named unnest. To name the column, use an alias, as in FROM unnest([1, 2]) AS t(x). A NULL element becomes a row with a NULL value, and an empty list returns no rows. To expand a list column, join its table to unnest of the column, as in CROSS JOIN unnest(table.list_column) AS t(element).

In the FROM clause, add WITH ORDINALITY after unnest(list) to return a second column that holds the position of each element in list, starting at 1. When you join a table to unnest of a list column, the positions start again at 1 for each row. Without an alias, the two columns are named unnest and ordinality.

In the SELECT list, unnest(list) turns each input row into one output row for each element of its list, and repeats the other columns of the row. A row whose list is empty or NULL produces no output rows. When the SELECT list has more than one unnest call, the lists are expanded side by side, and the shorter lists are padded with NULL.

In the SELECT list, unnest also expands a STRUCT value into one column for each field. With recursive := true, it expands every level of nested lists, and the STRUCT values inside them. With max_depth := n, it expands n levels of nested lists.

Syntax

FROM unnest(list) [ AS alias(column) ] FROM unnest(list) WITH ORDINALITY [ AS alias(column, ordinality_column) ] SELECT unnest(list [, recursive := recursive ] [, max_depth := max_depth ]) [ AS column ] SELECT unnest(struct)

Arguments

The unnest function takes the following arguments:

list

The list to expand, as a LIST value. In the FROM clause, pass exactly one list.

struct

The struct to expand into one column for each field, as a STRUCT value. The columns take the names of the fields. You can pass it only in the SELECT list.

recursive

A flag for recursive expansion, as a BOOLEAN value. If it is true, nested lists are expanded down to their innermost elements, and STRUCT elements are expanded into columns. The default is false. You can pass it only in the SELECT list. This argument is optional.

max_depth

The number of levels of nested lists to expand, as an INTEGER value. The default is 1. You can pass it only in the SELECT list. This argument is optional.

Return type

In the FROM clause, a table with one column whose type is the element type of list, such as INTEGER for an INTEGER[] list. When you add WITH ORDINALITY, the table also has a BIGINT column that holds the positions. In the SELECT list, values of the element type of list. With max_depth := n, the values have the type that is left after n levels of lists are expanded, and with recursive := true, they have the innermost element type. For a STRUCT value, one column for each field, of the type of that field.

Examples

The following SQL query uses unnest in the FROM clause to return one row for each element of a list, and names the column element.

SELECT element FROM unnest([3, 1, 2]) AS t(element) ORDER BY element

The query returns the following results.

element

1

2

3

The following SQL query uses unnest in the SELECT list to return one row for each tag of each input row. The input row whose id is 3 has an empty list, and the input row whose id is 4 has a NULL list, so neither produces output rows.

SELECT id, unnest(tags) AS tag FROM (VALUES (1, ['a', 'b']), (2, ['c']), (3, []), (4, NULL) ) AS v(id, tags) ORDER BY id, tag

The query returns the following results.

id tag

1

a

1

b

2

c

The following SQL query keeps the rows whose list is empty or NULL, by joining each row to unnest of its list with LEFT JOIN and ON true. Those rows get a NULL tag.

SELECT v.id AS id, t.tag AS tag FROM (VALUES (1, ['a', 'b']), (2, []), (3, NULL) ) AS v(id, tags) LEFT JOIN unnest(v.tags) AS t(tag) ON true ORDER BY id, tag

The query returns the following results.

id tag

1

a

1

b

2

NULL

3

NULL

The following SQL query expands two lists of different lengths side by side. The shorter list is padded with NULL.

SELECT unnest([1, 2, 3]) AS x, unnest(['a', 'b']) AS y ORDER BY x

The query returns the following results.

x y

1

a

2

b

3

NULL

The following SQL query adds WITH ORDINALITY when it joins each row to unnest of its list. The column n holds the position of each tag in its row's list, so it starts again at 1 for each id.

SELECT v.id AS id, t.tag AS tag, t.n AS n FROM (VALUES (1, ['x', 'y']), (2, ['z'])) AS v(id, tags) CROSS JOIN unnest(v.tags) WITH ORDINALITY AS t(tag, n) ORDER BY id, n

The query returns the following results.

id tag n

1

x

1

1

y

2

2

z

1

The following SQL query uses WITH ORDINALITY without a table alias, so the query refers to the two columns by their default names, unnest and ordinality.

SELECT unnest AS element, ordinality AS position FROM unnest(['a', 'b', 'c']) WITH ORDINALITY ORDER BY position

The query returns the following results.

element position

a

1

b

2

c

3

Considerations

In the FROM clause, unnest takes one list and no named parameters. To expand several lists side by side, call unnest once for each list in the SELECT list.

unnest does not accept a MAP value. To expand a map into rows, unnest its entries, as in unnest(map_entries(map)), which returns one STRUCT with the fields key and value for each entry.

You cannot use unnest inside an aggregate function or in a WHERE clause. Expand the list in a subquery, and then filter or aggregate in the outer query.

Differences from Athena engine version 3

In Athena engine version 3, UNNEST in the FROM clause accepts several arrays and expands them side by side, as in CROSS JOIN UNNEST(array1, array2) AS t(x, y). In DuckDB, unnest in the FROM clause takes one list. To expand several lists side by side, call unnest once for each list in the SELECT list, as in SELECT unnest(list1) AS x, unnest(list2) AS y.

In Athena engine version 3, UNNEST of a map returns one row for each entry, with a key column and a value column, as in CROSS JOIN UNNEST(map) AS t(k, v). In DuckDB, unnest does not accept a MAP value. Unnest the entries of the map instead, as in unnest(map_entries(map)), and read the key and value fields of each result.