for and let clauses compared

Although for and let clauses both bind variables, the manner in which variables are bound is different.

The following table provides examples that compare the results that are returned by FLWOR expressions that contain similar for and let clauses.

Table 1. Comparison of for and let clauses in FLWOR expressions
Description of query FLWOR expression Result
Bind a single variable using for
for $i in ("a", "b", "c")
return <output>{$i}</output>
<output>a</output>
<output>b</output>
<output>c</output>
Bind a single variable using let
let $i := ("a", "b", "c")
return <output>{$i}</output>
<output>a b c</output>
Bind multiple variables using for
for $i in ("a", "b"), $j in ("c", "d")
return <output>{$i, $j}</output>
<output>a c</output>
<output>b c</output>
<output>a d</output>
<output>b d</output>
Bind multiple variables using let
let $i := ("a", "b"), $j := ("c", "d")
return <output>{$i, $j}</output>
<output>a b c d</output>
Note: Because the expressions in this table do not include order by clauses, the order of the output elements is non-deterministic.