Hitachi

Hitachi Advanced Database SQL Reference


7.20.7 Comparison predicate

Comparison predicates can be specified in search conditions. The following example illustrates a comparison predicate.

Example:

From the sales history table (SALESLIST), this example retrieves the customer ID (USERID), product code (PUR-CODE), and date of purchase (PUR-DATE) for customers who purchased products on or after September 6, 2011.

SELECT "USERID","PUR-CODE","PUR-DATE"
    FROM "SALESLIST"
        WHERE "PUR-DATE">=DATE'2011-09-06'

Explanation

  • The underlined portion indicates the comparison predicate.

  • >= is called a comparison operator.

  • The terms on the left and right of a comparison operator are called comparison operands. In this example, the comparison operands are PUR-DATE (a column name) and DATE'2011-09-06' (a literal).

Organization of this subsection

(1) Specification format

comparison-predicate::= comparison-operand-1 comparison-operator comparison-operand-2
 
  comparison-operand::= {value-expression|row-value-constructor}
  comparison-operator::= {=|<>|!=|^=|<|<=|>|>=}

(2) Explanation of specification format

comparison-operand-1, comparison-operand-2:

The comparison operand is specified in the form of a value expression or a row value constructor. For details about value expressions, see 7.21 Value expression. For details about row value constructors, see 7.18 Row value constructors.

The specification rules for specifying row value constructors for comparison operand are as follows:

  • When specifying a row value constructor for a comparison operand, two or more row value constructor elements must be specified. If only one row value constructor element is specified, the row value constructor is treated as a value expression enclosed in parentheses.

    Example of being treated as a row value constructor

    ("C1","C2") = (1,2)

    Example of being treated as a value expression

    ("C1") = (1)

    The above is the same as "C1" = 1.

  • When specifying a row value constructor for a comparison operand, specify row value constructors for both the left and right comparison operands of the comparison operator.

comparison-operator:

The comparison operator is one of =, <>, !=, ^=, <, <=, >, or >=. The following table lists the meaning of each operator.

Table 7‒13: Meaning of comparison operators

No.

Comparison operator specification

Meaning

1

X = Y

X and Y are equal

2

X <> Y

X != Y

X ^= Y

X and Y are not equal

3

X < Y

X is less than Y

4

X <= Y

X is less than or equal to Y

5

X > Y

X is greater than Y

6

X >= Y

X is greater than or equal to Y

Legend:

X and Y: Comparison operands

Important

When specifying a row value constructor for a comparison operand, you can specify the comparison operators =, <>, !=, and ^= of No. 1 to 2 in the table above. The comparison operators No. 3 to 6 cannot be specified.

(3) Evaluation of predicates

■Specifying a value expression to a comparison operand

It is True if the comparison operands on either side of the comparison operator satisfy the comparison conditions.

It is undefined if either of the comparison operands are a null value.

■Specifying a row value constructor to a comparison operand

The value of each row value constructor element on both sides of the operands of the comparison operator is compared to determine the result.

(Example)

[Figure]

For comparison operator "="

True if all the values of the row value constructor elements being compared are equal.

False if one or more of the values of the row value constructor elements being compared are not equal.

Undefined if the row value constructor elements has one or more null values and all the values of the row value constructor elements being compared (except the values of the row value constructor element to which the null value and null value are being compared) are equal.

(Example)

Specification example of comparison predicate

Comparison predicate result

(1,2,3) = (1,2,3)

True

(1,2,3) = (1,2,4)

False

(1,2,3) = (1,null value,4)

False

(1,2,3) = (1,2,null value)

Undefined

(1,2,null value) = (1,2,null value)

Undefined

For comparison operators "<>", "!=", "^="

True if one or more of the values of the row value constructor elements being compared are not equal.

False if all the values of the row value constructor elements being compared are equal.

Undefined if the row value constructor elements has one or more null values and all the values of the row value constructor elements being compared (except the values of the row value constructor element to which the null value and null value are being compared) are equal.

(Example)

Specification example of comparison predicate

Comparison predicate result

(1,2,3) <> (1,2,4)

True

(1,2,3) <> (1,null value,4)

True

(1,2,3) <> (1,2,3)

False

(1,2,3) <> (1,2,null value)

Undefined

(1,2,null value) <> (1,2,null value)

Undefined

(4) Rules

(a) Rules for specifying a value expression to a comparison operand

  1. The data types of the results of comparison-operand-1 and comparison-operand-2 must be data types that can be compared. For details about data that can be compared, see (1) Data types that can be compared in 6.2.2 Data types that can be converted, assigned, and compared.

  2. If the result of a comparison operand is the null value, the comparison result is unknown.

  3. If you specify binary data or UUID data in the value expression of a comparison operand, only =, <>, !=, or ^= can be specified as the comparison operator.

  4. Array and STRUCT type data cannot be specified for the value expression of a comparison operand.

  5. A comparison operand composed of only a dynamic parameter cannot appear on both sides of the comparison operator.

    • Example of a specification that is not permitted:?=?

    • Examples of a permissible specifications: "C1"=?, ?=10

  6. For comparison-operand-2 you cannot specify the scalar function CONTAINS.

  7. If you specify the scalar function CONTAINS for comparison-operand-1, specify > as the comparison operator. In this case, specify 0 for comparison-operand-2.

(b) Rules for specifying a row value constructor for comparison operands

  1. Make sure that the row value constructor specifying the left and right comparison operators has the same number of row value constructor elements.

    Example of correct specification: ("C1","C2","C3") = (1,2,3)

    Example of incorrect specification: ("C1","C2","C3") = (1,2,3,4)

  2. The combination of data types of each row value constructor element of the row value constructor specified to the left and right of the comparison operator should be of data types that can be compared. For details about data that can be compared, see (1) Data types that can be compared in 6.2.2 Data types that can be converted, assigned, and compared.

  3. Each row value constructor element of the row value constructor specified on either side of the comparison operator must contain a dynamic parameter.

    Example of correct specification: ("C1",?,"C3") = (1,2,?)

    Example of incorrect specification: ("C1",?,"C3") = (1,?,3)

  4. If a dynamic parameter is specified in a row value constructor element, the data type of the dynamic parameter is assumed to be the data type of the row value constructor element that is the comparison partner.

  5. Of the row value constructor elements specified on either side of the comparison operator, specify at least one row value constructor whose row value constructor element is only a value specification.

    Example of correct specification: ("C1","C2","C3") = (1,2,3)

    Example of incorrect specification: ("C1","C2","C3") = ("C4","C5","C6")

  6. Array type and STRUCT type data cannot be specified for the row value constructor elements of the row value constructors specified on either side of the comparison operator.

  7. The following value expressions cannot be specified in the row value constructor elements of the row value constructors specified on either side of the comparison operator

    • Set functions

    • Scalar subquery

    • Array element reference

  8. A comparison predicate with a row value constructor can be specified for either of the following.

    • WHERE clause

    • WHERE search condition in a DELETE or UPDATE statement

  9. An comparison predicate with a row value constructor cannot be specified in the search conditions of a CASE expression.

(5) Examples

Example 1

From the sales history table (SALESLIST), this example retrieves the customer ID (USERID), product code (PUR-CODE), and date of purchase (PUR-DATE) for customers who purchased product code P001 or P003 on or after September 4, 2011.

SELECT "USERID","PUR-CODE","PUR-DATE"
    FROM "SALESLIST"
        WHERE "PUR-DATE">=DATE'2011-09-04'
        AND ("PUR-CODE"='P001' OR "PUR-CODE"='P003')

The underlined portions show the comparison predicates.

The comparison operand specifies a value expression.

Example 2

Retrieve the customer ID (USERID) U001 and product code (PUR-CODE) P001 from the sales history table (SALESLIST).

SELECT "USERID","PUR-CODE","PUR-NUM"
  FROM "SALESLIST"
  WHERE ("USERID","PUR-CODE") = ('U001','P001')

The underlined portions show the comparison predicates.

The comparison operand specifies a row value constructor.