Unwind
UNWIND
allows you to unnest or explode a list L
that has k
elements in it,
into a table T
with k
elements.
When using UNWIND
, you need to specify an alias to refer to the elements
of the exploded list, i.e., T
.
We will use the example database for demonstration, whose schema and data import commands are given here.
For example, the following explodes a list of strings into multiple rows and returns its individual elements:
Output:
If the UNWIND
ed list L contained as elements other lists,
then the output would be elements with one fewer nesting. For example, if L was a list of
list of strings, after unwinding, you’d get a table of list of strings. For example:
Output:
Using UNWIND
with WHERE
predicates
You can also use the WHERE
predicate with UNWIND
. For example, say you want to return the
elements of a given list that are greater than 2.
You might think that the following query will work:
However, this will throw a parser error:
The use of WHERE
predicates directly after UNWIND
is not allowed. You can attach a WITH
clause
immediately after UNWIND
to achieve the result.
Output: