Order of results in XQuery expressions

When using Db2 XQuery, some kinds of XQuery expressions return sequences in a deterministic order while other kinds of expressions return sequences in a non-deterministic order.

The following kinds of expressions return sequences in a deterministic order:
  • FLWOR expressions that contain an explicit order by clause return results in the order specified. For example, the following expression returns a sequence of product elements in ascending order by price:
    for $p in /product
    order by $p/price
    return $p
  • Updating expressions that add elements and use explicit position keywords return results with the added elements in the position specified. For example, the following updating expression uses the as first into keywords and inserts the element <status>current</status> as the first item element in the customerinfo element:
    xquery
    transform
    copy $mycust := db2-fn:sqlquery('select info from customer where cid = 1001') 
    modify 
      do insert <status>current</status> as first into $mycust/customerinfo
    return $mycust
  • Expressions that combine sequences with the union, intersect, or except operator return results in document order.
  • Path expressions that satisfy the following conditions return results in document order:
    • The path expression contains only forward-axis steps.
    • The path expression has its origin in a single node, such as might result from a function call or a variable reference.
    • No step in the path expression contains more than a single predicate.
    • The path expression does not contain a fn:position function call or a fn:last function call.
    The following example is a path expression that returns results in document order, assuming that the variable $bib is bound to a single element.
    $bib/book[title eq "War and Peace"]/chapter
  • Range expressions, which are expressions that contain the to operator, return sequences of integers in ascending order. For example: 15 to 25.
  • Expressions that contain comma operators, if all the operands are sequences with deterministic order, return results in the order of their operands. For example the following expression returns the sequence (5, 10, 15, 16, 17, 18, 19, 20, 25):
    (5, 10, 15 to 20, 25)
  • Other expressions that contain operand expressions that return results in deterministic order return results in a deterministic order. For example, assuming the variable $pub is bound to a single element, the following conditional expression returns ordered results because the path expressions in the then and else clauses return ordered results:
    if ($pub/type eq "journal")
      then $pub/editor
      else $pub/author

If an expression that is not listed in the previous list returns more than one item, the order of items in the sequence is nondeterministic.

Table 1. Summary of ordering of results in XQuery expressions
Expression kind Conditions for a deterministic ordering Ordering of results Example
FLWOR Explicit order by clause Determined by the order by clause The following expression returns a sequence of product elements in ascending order by price:
for $p in /product
order by $p/price
return $p
Updating expressions Use of keywords that specify a position when adding an element Determined by the updating expression keywords When inserting an element using the keywords as last into, the element is added as the last child of the specified node.
Expressions with union, intersect, or except operators None Document order $managers union $students
Path expressions
  • The path expression contains only forward-axis steps.
  • The path expression has its origin in a single node, such as might result from a function call or a variable reference.
  • No step in the path expression contains more than a single predicate.
  • The path expression does not contain a fn:position function call or a fn:last function call.
Document order The following example is a path expression that returns results in document order, assuming that the variable $bib is bound to a single element.
$bib/book
[title eq
"War and Peace"]
/chapter
Range expressions, which are expressions that contain the to operator None Sequence of integers in ascending order 15 to 25
Expressions that contain comma operators All the operands are sequences with deterministic order Return results are in the order of their operands (5, 10, 15 to 20, 25)
Other expressions Operand expressions all return results that are in a deterministic order Determined by the ordering of the results of the nested expressions Assuming the variable $pub is bound to a single element, the following conditional expression returns ordered results because the path expressions in the then and else clauses return ordered results:
if ($pub/type
eq "journal")
then $pub/editor
else $pub/author
Note: If a positional predicate is applied to a sequence that does not have a deterministic order, the result is nondeterministic, which means that any item in the sequence can be selected.