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, 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 | Aggregate: the values of a group of rows as one list |
| FIRST, LAST | The first or last element |
| AT | The element at a zero-based position |
| TAIL | The list without its first element |
| SIZE | The number of elements |
| RANGE | A list of integers |
| LIST_COMP | A new list with an expression applied to every element |
| 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:
SELECT collect_list(p.name ORDER BY p.name) AS product_namesFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';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
Section titled “COLLECT_LIST”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 BYthe order of the elements is not defined. TheORDER BYexpression 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:
SELECT order_id, collect_list(product_id ORDER BY product_id) AS products, collect_list(quantity) AS quantitiesFROM order_itemsWHERE order_id < 4GROUP BY order_idORDER 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:
SELECT collect_list(p.unit_price ORDER BY p.unit_price DESC) AS pricesFROM 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:
SELECT customer_id, collect_list(shipped_date ORDER BY shipped_date) AS shipped, COUNT(*) AS ordersFROM ordersWHERE customer_id = 31GROUP BY customer_id;| customer_id | shipped | orders |
|---|---|---|
| 31 | {2022-01-29, 2025-12-17} | 3 |
FIRST ( list )Returns the first element of list. Return type: the type of the elements.
LAST ( list )Returns the last element of list. Return type: the type of the elements.
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:
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 productsFROM 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:
SELECT b.name AS brand, size(collect_list(p.id)) AS products, first(collect_list(p.name ORDER BY p.name)) AS first_nameFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name IN ('Diadora', 'Rab', 'Reebok', 'Wilson')GROUP BY b.nameORDER 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.
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.
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):
SELECT AT(collect_list(p.unit_price ORDER BY p.unit_price DESC), 2) AS third_highestFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';| third_highest |
|---|
| 68.00 |
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
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 ( 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
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 ( 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
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
Section titled “LIST_COMP”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
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:
SELECT list_comp(x IN collect_list(p.unit_price ORDER BY p.unit_price DESC) | ROUND(x * 0.9, 2)) AS sale_pricesFROM 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
Section titled “REDUCE”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.
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:
SELECT reduce(v = 0, x IN collect_list(p.units_in_stock) | v + x) AS units, SUM(p.units_in_stock) AS check_sumFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';| units | check_sum |
|---|---|
| 282 | 282 |