---
title: "List functions: COLLECT_LIST, FIRST, LAST, AT, TAIL, SIZE, RANGE, LIST_COMP, REDUCE"
description: "Build and process lists in CoraDB SQL: COLLECT_LIST, FIRST, LAST, AT, TAIL, SIZE, RANGE, and LIST_COMP and REDUCE."
url: "https://www.coradb.ai/manual/sql/function/list_fn.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/list_fn.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb collect_list, coradb list functions, list_comp, list comprehension sql, reduce sql, range sql, first last tail at size, sequence functions, graph aggregation"
---

# List functions: COLLECT_LIST, FIRST, LAST, AT, TAIL, SIZE, RANGE, LIST_COMP, REDUCE

List functions build and process ordered collections (`SEQUENCE` values, also called lists). They work the same way in relational queries and in graph queries with [MATCH](https://www.coradb.ai/manual/graph/match.html), and they combine well with each other: `COLLECT_LIST` turns rows into a list, and the other functions read or transform it.

| Function | Returns |
|---|---|
| [COLLECT_LIST](https://www.coradb.ai/manual/sql/function/list_fn.html#collect_list) | Aggregate: the values of a group of rows as one list |
| [FIRST](https://www.coradb.ai/manual/sql/function/list_fn.html#first), [LAST](https://www.coradb.ai/manual/sql/function/list_fn.html#last) | The first or last element |
| [AT](https://www.coradb.ai/manual/sql/function/list_fn.html#at) | The element at a zero-based position |
| [TAIL](https://www.coradb.ai/manual/sql/function/list_fn.html#tail) | The list without its first element |
| [SIZE](https://www.coradb.ai/manual/sql/function/list_fn.html#size) | The number of elements |
| [RANGE](https://www.coradb.ai/manual/sql/function/list_fn.html#range) | A list of integers |
| [LIST_COMP](https://www.coradb.ai/manual/sql/function/list_fn.html#list_comp) | A new list with an expression applied to every element |
| [REDUCE](https://www.coradb.ai/manual/sql/function/list_fn.html#reduce) | One value computed from all elements |

Collection literals are written in braces: `{1, 2, 3}`. In corasql, list results are displayed the same way.

The graph examples use the brand **Diadora**, which manufactures nine products:

```sql
SELECT collect_list(p.name ORDER BY p.name) AS product_names
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name = 'Diadora';
```

```text
product_names
==========================================================================
{'Abominable Hoodie', 'Beaumont Summit Kit', 'Bess Yoga Short', 'Circe Hooded Ice Fleece', 'Gobi HeatTec Tee', 'Helena Hooded Fleece', 'Kenobi Trail Jacket', 'Neve Studio Dance Jacket', 'Viktor LumaTech Pant'}
```


## COLLECT_LIST

```sql
COLLECT_LIST ( expression [ ORDER BY expression [ASC | DESC] ] )
```

An aggregate function that returns the values of *expression* in a group of rows as one list. Like other aggregate functions it can be used with or without `GROUP BY`.

- **NULL** values are skipped. If the group has no rows, or only **NULL** values, the result is **NULL**.
- Without `ORDER BY` the order of the elements is not defined. The `ORDER BY` expression must be the aggregated expression itself (or its position, `1`): `COLLECT_LIST(p.name ORDER BY p.name)` is valid, `COLLECT_LIST(p.name ORDER BY p.unit_price)` is an error.

**Return type:** SEQUENCE

The products and quantities of the first four orders, one list per order:

```sql
SELECT order_id,
       collect_list(product_id ORDER BY product_id) AS products,
       collect_list(quantity) AS quantities
FROM order_items
WHERE order_id < 4
GROUP BY order_id
ORDER BY order_id;
```

| order_id | products | quantities |
|---|---|---|
| 0 | {123, 1274, 1379, 1634} | {1, 1, 1, 1} |
| 1 | {1137} | {2} |
| 2 | {1575, 1619} | {2, 4} |
| 3 | {607, 1207, 1634} | {1, 1, 12} |

The prices of Diadora's products, highest first:

```sql
SELECT collect_list(p.unit_price ORDER BY p.unit_price DESC) AS prices
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name = 'Diadora';
```

| prices |
|---|
| {69.00, 69.00, 68.00, 55.00, 47.00, 46.00, 42.00, 29.00, 28.00} |

NULL values are not collected. Customer 31 has three orders, but only two have been shipped:

```sql
SELECT customer_id,
       collect_list(shipped_date ORDER BY shipped_date) AS shipped,
       COUNT(*) AS orders
FROM orders
WHERE customer_id = 31
GROUP BY customer_id;
```

| customer_id | shipped | orders |
|---|---|---|
| 31 | {2022-01-29, 2025-12-17} | 3 |


## FIRST

```sql
FIRST ( list )
```

Returns the first element of *list*. **Return type:** the type of the elements.


## LAST

```sql
LAST ( list )
```

Returns the last element of *list*. **Return type:** the type of the elements.

> **Warning:**
> `FIRST` and `LAST` of an empty list raise the error *Sequence index 0 is out of bounds*. Check the list with [SIZE](https://www.coradb.ai/manual/sql/function/list_fn.html#size) first if it can be empty.


```sql
SELECT first({1, 2, 3}) AS a, last({1, 2, 3}) AS b;
```

| a | b |
|---|---|
| 1 | 3 |

Combined with `COLLECT_LIST ... ORDER BY`, they return the extremes of a group, here Diadora's highest and lowest price:

```sql
SELECT first(collect_list(p.unit_price ORDER BY p.unit_price DESC)) AS highest,
       last(collect_list(p.unit_price ORDER BY p.unit_price DESC)) AS lowest,
       size(collect_list(p.id)) AS products
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name = 'Diadora';
```

| highest | lowest | products |
|---|---|---|
| 69.00 | 28.00 | 9 |

With `GROUP BY`, one value per group. For four brands, the number of products and the alphabetically first product name:

```sql
SELECT b.name AS brand,
       size(collect_list(p.id)) AS products,
       first(collect_list(p.name ORDER BY p.name)) AS first_name
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name IN ('Diadora', 'Rab', 'Reebok', 'Wilson')
GROUP BY b.name
ORDER BY b.name;
```

| brand | products | first_name |
|---|---|---|
| Diadora | 9 | Abominable Hoodie |
| Rab | 11 | Ajax Full-Zip Sweatshirt |
| Reebok | 11 | Abominable Hoodie |
| Wilson | 11 | Apollo Running Short |

With a path variable, `FIRST(NODES(path))` and `LAST(NODES(path))` return the start and end vertex of a path; see [Graph functions](https://www.coradb.ai/manual/sql/function/graph_fn.html).


## AT

```sql
AT ( list, index )
```

Returns the element of *list* at position *index*. Positions start at **0**. An index that is negative or not smaller than the size of the list raises the error *Sequence index n is out of bounds*.

**Return type:** the type of the element.

```sql
SELECT AT({1, 2, 3}, 1) AS a, AT(AT({ {1, 2}, {3, 4}, {5, 6} }, 1), 0) AS b;
```

| a | b |
|---|---|
| 2 | 3 |

The second call reads element 1 of the outer list, `{3, 4}`, and then its element 0.

Diadora's third-highest price (position 2):

```sql
SELECT AT(collect_list(p.unit_price ORDER BY p.unit_price DESC), 2) AS third_highest
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name = 'Diadora';
```

| third_highest |
|---|
| 68.00 |


## TAIL

```sql
TAIL ( list )
```

Returns *list* without its first element. For a list with one element or an empty list, it returns an empty list. Nest it to skip more elements: `TAIL(TAIL(list))` skips two.

**Return type:** SEQUENCE

```sql
SELECT TAIL({1, 2, 3}) AS a, TAIL({1}) AS b, TAIL(TAIL({ {1, 2}, {3, 4}, {5, 6} })) AS c;
```

| a | b | c |
|---|---|---|
| {2, 3} | {} | {{5, 6}} |


## SIZE

```sql
SIZE ( list )
```

Returns the number of elements in *list*, including duplicates and **NULL** elements. An empty list has size 0. For a list of lists, only the outer elements are counted.

**Return type:** INTEGER

```sql
SELECT size({2, 2, 3, 3}) AS a, size({{1, 2}, {3, 4}, {5, 6}}) AS b, size({}) AS c;
```

| a | b | c |
|---|---|---|
| 4 | 3 | 0 |


## RANGE

```sql
RANGE ( start, end, step )
```

Returns the integers from *start* to *end*, both included, in steps of *step*. A negative *step* counts down. If *step* points away from *end*, the result is an empty list; if *step* is larger than the distance, only *start* is returned. Non-integer arguments are rounded to the nearest integer.

**Return type:** SEQUENCE of INTEGER

```sql
SELECT range(1, 5, 1) AS a, range(5, 1, -1) AS b, range(1.5, 11, 3) AS c, range(1, 5, -1) AS d, range(-5, 5, 11) AS e;
```

| a | b | c | d | e |
|---|---|---|---|---|
| {1, 2, 3, 4, 5} | {5, 4, 3, 2, 1} | {2, 5, 8, 11} | {} | {-5} |

In `c`, the start value 1.5 is rounded to 2.


## LIST_COMP

```sql
LIST_COMP ( variable IN list | expression )
```

List comprehension: evaluates *expression* once for every element of *list*, with *variable* standing for the element, and returns the results as a new list in the same order. An empty list gives an empty list.

**Return type:** SEQUENCE

```sql
SELECT list_comp(x IN {1, 2, 3} | x * 2) AS a,
       list_comp(x IN range(1, 5, 1) | x * x) AS b,
       list_comp(x IN {'a', 'bb', 'ccc'} | CHAR_LENGTH(x)) AS c;
```

| a | b | c |
|---|---|---|
| {2, 4, 6} | {1, 4, 9, 16, 25} | {1, 2, 3} |

Diadora's prices with a 10% discount:

```sql
SELECT list_comp(x IN collect_list(p.unit_price ORDER BY p.unit_price DESC) | ROUND(x * 0.9, 2)) AS sale_prices
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name = 'Diadora';
```

| sale_prices |
|---|
| {62.10, 62.10, 61.20, 49.50, 42.30, 41.40, 37.80, 26.10, 25.20} |


## REDUCE

```sql
REDUCE ( accumulator = initial_value, variable IN list | expression )
```

Folds *list* into one value. *accumulator* starts at *initial_value*; for every element, *expression* (which can use *accumulator* and *variable*) is evaluated and becomes the new value of *accumulator*. The final value is returned. For an empty list, the result is *initial_value*.

If *expression* returns **NULL** for any element, the following steps start from **NULL**: with `v + x`, one **NULL** element makes the result **NULL**.

**Return type:** the type of *expression*.

```sql
SELECT reduce(v = 0, x IN {1, 2, 3, 4, 5} | v + x) AS a, reduce(v = 0, x IN {1, NULL, 3} | v + x) AS b;
```

| a | b |
|---|---|
| 15 | NULL |

The units in stock of all Diadora products; the result matches `SUM`:

```sql
SELECT reduce(v = 0, x IN collect_list(p.units_in_stock) | v + x) AS units,
       SUM(p.units_in_stock) AS check_sum
FROM MATCH (b:brand)-[m:manufactures]->(p:product)
WHERE b.name = 'Diadora';
```

| units | check_sum |
|---|---|
| 282 | 282 |

## See also

- [Graph functions (NODES, RELATIONSHIPS, path comprehension)](https://www.coradb.ai/manual/sql/function/graph_fn.html)
- [List predicates (ALL, ANY, NONE, SINGLE)](https://www.coradb.ai/manual/sql/function/predicate_fn.html)
- [Aggregate and analytic functions](https://www.coradb.ai/manual/sql/function/analysis_fn.html)
- [Collection data types](https://www.coradb.ai/manual/sql/datatype_collection.html#collection-types)
