Syntax of FLWOR expressions

A FLWOR expression is composed of the following clauses, some of which are optional: for, let, where, order by, and return.

Read syntax diagramSkip visual syntax diagramfor-clauselet-clausewhereexpression3order by,expression4ascendingdescendingreturnexpression5
for-clause
Read syntax diagramSkip visual syntax diagramfor ,$ variable-name1at$ positional-variable-nameinexpression1
let-clause
Read syntax diagramSkip visual syntax diagramlet ,$ variable-name2:=expression2
for
The keyword that begins a for clause. A for clause iterates over the result of expression1 and binds variable-name1 to each item that is returned by expression1.
let
The keyword that begins a let clause. A let clause binds variable-name2 to the entire result of expression2.
variable-name1, variable-name2
The name of the variable to bind to the result of expression1 or expression2.
positional-variable-name
The name of an optional variable that is bound to the position within the input stream of the item that is bound by each iteration of the for clause.
expression1, expression2, expression3, expression4, expression5
Any XQuery expression. If the expression includes a top-level comma operator, then the expression must be enclosed in parentheses.
where
The keyword that begins a where clause. A where clause filters the tuples of variable bindings that are generated by the for and let clauses.
order by
The keywords that begin an order by clause. An order by clause specifies the order in which values are processed by the return clause.
ascending
Specifies that ordering keys are processed in ascending order.
descending
Specifies that ordering keys are processed in descending order.
return
The keyword that begins a return clause. The expression in the return clause is evaluated once for each tuple of bound variables that is generated by the for, let, where, and order by clauses. The results of all of the evaluations of the return clause are concatenated into a single sequence, which is the result of the FLWOR expression.