General comparisons in XQuery

General comparisons compare two sequences of any length to determine if a comparison is true for at least one item in both sequences. The general comparison operators include =, !=, <, <=, >, and >=.

The following table describes these operators, listed in order of operator precedence from highest to lowest.
Table 1. General comparison operators in XQuery
Operator Purpose
= Returns true if any value in the first sequence is equal to any value in the second sequence.
!= Returns true if any value in the first sequence is not equal to any value in the second sequence.
< Returns true if any value in the first sequence is less than any value in the second sequence.
<= Returns true if any value in the first sequence is less than or equal to any value in the second sequence.
> Returns true if any value in the first sequence is greater than any value in the second sequence.
>= Returns true if any value in the first sequence is greater than or equal to any value in the second sequence.
Restriction: The operand of a general comparison cannot contain an FLWOR expression.
The result of a general comparison expression is a boolean value or an error. XQuery uses the following process to evaluate a general comparison expression.
  1. Atomizes each operand into a sequence of atomic values.
  2. Compares each of the values in the first sequence to each of the values in the second sequence. For each comparison:
    • If one of the atomic values is an instance of xs:untypedAtomic and the other is an instance of a numeric type (xs:integer, xs:decimal, or xs:double), the untyped value is cast to the type xs:double.
    • If one of the atomic values is an instance of xs:untypedAtomic and the other is an instance of xs:untypedAtomic or xs:string, the xs:untypedAtomic values are cast to the type xs:string.
    • If one of the atomic values is an instance of xs:untypedAtomic and the other is not an instance of xs:string, xs:untypedAtomic, or any numeric type, the xs:untypedAtomic value is cast to the dynamic type of the other value.
  3. If at least one of the values in the first sequence and at least one of the values in the second sequence meet the conditions of the comparison, the general comparison is true.

Syntax

comparison expression

Read syntax diagramSkip visual syntax diagramarithmetic-expression =!=<><=>=arithmetic-expression

Examples

The following query uses a general comparison expression to find the descriptions of products that cost less than 20 units.

SELECT XMLQUERY ('declare namespace pos="http://posample.org";
  /pos:product/description[price < 20]'
  PASSING DESCRIPTION)
FROM T1;