Logical expressions

Logical expressions return the boolean value true if both of two expressions are true, or if one or both of two expressions are true. The operators that are used in logical expressions include and and or.

The following table describes these operators, listed in order of operator precedence from highest to lowest.
Table 1. Logical expression operators in XQuery
Operator Purpose
and Returns true if both expressions are true.
or Returns true if one or both expressions are true.
Restriction: The operand of a logical expression cannot contain an FLWOR expression.
The result of a logical expression is a boolean value or an error. XQuery uses the following process to evaluate a logical expression.
  1. Determines the effective boolean value (EBV) of each operand.
  2. Applies the operator to the effective boolean values of the operands. The result is a boolean value or an error. Table 2 shows the results that are returned by a logical expression based on the EBV of its operands and any errors that are encountered during the evaluation of an operand.
Table 2. Results of logical expressions based on effective boolean values (EBVs) of operands
EBV of operand 1 Operator EBV of operand 2 Result
true and true true
true and false false
false and true false
false and false false
true and error error
error and true error
false and error false or error
error and false false or error
error and error error
true or true true
false or false false
true or false true
false or true true
true or error true or error
error or true true or error
false or error error
error or false error
error or error error

Syntax

logical expression

Read syntax diagramSkip visual syntax diagramexpression andorexpression

Examples

The following query uses a logical expression to retrieve records from a table for 22-inch snow shovels or 24-inch snow shovels.
SELECT XMLQUERY ('declare namespace pos="http://posample.org";
  /pos:product/description[name = "Snow Shovel, Deluxe 24"""
  or name = "Snow Shovel, Basic 22"""]'
  PASSING DESCRIPTION) 
FROM T1;