Skip to main content

Subqueries

Wherever an expression is allowed in C8QL, a subquery can be placed. A subquery is a query part that can introduce its own local variables without affecting variables and values in its outer scope(s).

It is required that subqueries be put inside parentheses ( and ) to explicitly mark their start and end points:

FOR p IN persons
LET recommendations = (
FOR r IN recommendations
FILTER p.id == r.personId
SORT p.rank DESC
LIMIT 10
RETURN r
)
RETURN { person : p, recommendations : recommendations }
FOR p IN persons
COLLECT city = p.city INTO g
RETURN {
city : city,
numPersons : LENGTH(g),
maxRating: MAX(
FOR r IN g
RETURN r.p.rating
)}

Subqueries may also include other subqueries.

Note that subqueries always return a result array, even if there is only a single return value:

RETURN ( RETURN 1 )
[ [ 1 ] ]

To avoid such a nested data structure, FIRST() can be used for example:

RETURN FIRST( RETURN 1 )
[ 1 ]