Hitachi

Hitachi Advanced Database SQL Reference


7.20.3 IN predicate

The IN predicate is used for retrieving data that satisfies any one condition value out of multiple condition values.

Organization of this subsection

(1) Specification format

IN-predicate::= {value-expression|row-value-constructor} [IS] [NOT] IN
               {({value-expression|row-value-constructor} [,{value-expression|row-value-constructor}]...)|table-subquery}

(2) Explanation of specification format

{value-expression|row-value-constructor}:

Specifies what the IN predicate is being evaluated for in the form of a value expression or row value constructor to the left of the IN predicate.

The conditional value to be evaluated and compared is specified to the right of the IN predicate in the form of a value expression or row value constructor.

The meaning of the value expression or row value constructor to the left (right) of the IN predicate is shown below.

[Figure]

  • For value expression

    In the figure above, value-expression-1 is the value expression on the left side of the IN predicate (the target of evaluation of the IN predicate), value-expression-2 and value-expression-3 are the value expressions on the right side of the IN predicate (which are the conditional values to be evaluated and compared). For details about value expressions, see 7.21 Value expression.

  • For row value constructor

    In the figure above, row-value-constructor-1 is the row value constructor on the left side of the IN predicate (the target of evaluation of the IN predicate), row-value-constructor-2 and row-value-constructor-3 are the row value constructors on the right side of the IN predicate (which are the conditional values to be evaluated and compared). For details about row value constructors, see 7.18 Row value constructors.

Note the following rules:

  • When specifying a row value constructor on either side of a IN predicate, 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") IN ((1,'A'),(2,'B'))

    Example of being treated as a value expression

    ("C1") IN ((1),(2))

    The above is the same as ""C1" IN (1,2)".

  • If you specify a value expression to the left of the IN predicate, specify only a value expression or a table subquery to the right of the IN predicate.

  • If a row value constructor is specified to the left of a IN predicate, only the row value constructor should be specified to the right of the IN predicate.

  • Up to 30,000 value expressions or row value constructors can be specified to the right of the IN predicate.

IS:

You cannot omit the IS specification. The results will be the same regardless of whether it is specified.

NOT:

If NOT is specified, values that do not match the value expressions specified following the IN predicate will become the target of retrieval.

If NOT is not specified, values that match the value expressions specified following the IN predicate will become the target of retrieval.

table-subquery:

Specifies a table subquery. For details about table subqueries, see 7.3 Subqueries.

Note that if you specify a table subquery in an IN predicate, a work table might be created. If the size of the work table DB area where the work table will be created has not been estimated correctly, it might result in performance degradation. For details about estimating the size of the work table DB area, see the HADB Setup and Operation Guide. For details about work tables, see Considerations when executing an SQL statement that creates work tables in the HADB Application Development Guide.

(3) Evaluation of predicates

The conditions under which the result of the IN predicate is True are shown in the following table.

Table 7‒5: Conditions under which the result of an IN predicate is True

NOT specification

Specification value of IN predicate

Specification value right of IN predicate

Conditions under which the result of an IN predicate is True

Not specified

Value expression

Value expression

The specified value on the left side of the IN predicate matches one or more of the specified values on the right side

Row value constructors

Row value constructors

Value expression

Table subquery

The value expression on the left side of the IN predicate matches one or more of the result rows of the table subquery on the right

Specified

Value expression

Value expression

The specified value on the left side of the IN predicate does not match all the specified values on the right side

Row value constructors

Row value constructors

Value expression

Table subquery

The value expression on the left side of the IN predicate does not match all the result lines of the table subquery on the right

The result of an IN predicate without NOT is the same as the result of a search condition connected by an OR condition, and the result of an IN predicate with NOT is the same as the result of a search condition connected by an AND condition. The following table shows the search conditions that produce the same result as the specification of the IN predicate.

Table 7‒6: Search conditions that produce the same result as the IN predicate specification

IN predicate specification

Search conditions that produce the same results as those specified to the left

value-expression-1 IN (value-expression-2,value-expression-3,…)*

(value-expression-1 = value-expression-2) OR (value-expression-1 = value-expression-3) OR ...

row-value-constructor-1 IN (row-value-constructor-2,row-value-constructor-3,...)

(row-value-constructor-1 = row-value-constructor-2) OR (row-value-constructor-1 = row-value-constructor-3) OR ...

value-expression-1 NOT IN (value-expression-2,value-expression-3,…)*

(value-expression-1 <> value-expression-2) AND (value-expression-1 <> value-expression-3) AND …

row-value-constructor-1 NOT IN (row-value-constructor-2,row-value-constructor-3,…)

(row-value-constructor-1 <> row-value-constructor-2) AND (row-value-constructor-1 <> row-value-constructor-3) AND …

#

If you specify a table-subquery to the right of the IN predicate, read it as if each result line of that table subquery were (value-expression-2, value-expression-3,...).

The following table shows an example of a IN predicate specification and result.

Table 7‒7: Example of IN predicate specification and result

Example of IN predicate specification

IN predicate result

1 IN (1,2,3)

True

1 IN (1,2,null value)

True

1 IN (2,3,4)

False

1 IN (2,3,null value)

Undefined

(1,2) IN ((1,2),(2,3),(3,4))

True

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

True

(1,2) IN ((2,3),(3,4),(4,5))

False

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

Undefined

1 NOT IN (2,3,4)

True

1 NOT IN (1,2,3)

False

1 NOT IN (1,2,null value)

False

1 NOT IN (2,3,null value)

Undefined

(1,2) NOT IN ((2,3),(3,4),(4,5))

True

(1,2) NOT IN ((1,2),(2,3),(3,4))

False

(1,2) NOT IN ((1,2),(2,3),(1,null value))

False

(1,2) NOT IN ((1,null value),(2,3),(3,4))

Undefined

(4) Rules

(a) Rules for specifying value expression on left side of IN predicate

  1. You cannot specify a dynamic parameter alone for a value expression on the left side of a IN predicate.

  2. Specify each value expression such that the data type of the result of the value expressions on the left side of the IN predicate can be compared to the data types of the results of the value expressions on the right side of the IN. For details about data types 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.

    However, if you specify the predefined input representation of a date, time, or time stamp for the value expressions on the left side of the IN predicate, you cannot specify datetime data for the value expressions on the right side of the IN predicate. For information about predefined input representations, see 6.3.3 Predefined character-string representations.

  3. If the result of the value expression on the left side of the IN predicate is a null value, the result of the IN predicate is undefined. Also, if the results and all value expressions to the right side of the IN predicate are null values, the result of the IN predicate is undefined.

  4. You cannot specify array data and structure data for the value expression on both sides of the IN predicate.

  5. If a table subquery returns zero rows (empty set), the result of the IN predicate is false. However, if NOT is specified, the result evaluates to true.

  6. When you specify a table subquery, specify only one selection expression in the table subquery.

  7. The IN predicate with a table subquery specified is rewritten by HADB into a quantified predicate (=ANY specification) and then processed.

  8. You cannot specify array data or structure data for a value expression of a selection expression of a table subquery.

(b) Rules for specifying a row value constructor on the left side of an IN predicate

  1. Make sure that each row value constructor to the left and right of the IN predicate has the same number of row value constructor elements.

    Example of correct specification: ("C1","C2") IN ((1,'A'),(2,'B'))

    Example of incorrect specification: ("C1","C2") IN ((1,'A'),(2,'B','C'))

  2. Make sure that the data types of the results of each row value constructor element on the left side of the IN predicate and the data types of the results of each row value constructor element on the right side of the IN predicate are comparable. For details about data types 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.

    Example:

    [Figure]

    However, if you specify a predefined input representation that expresses a date, time, or time stamp on the left side row value constructor element of the row value constructor of the IN predicate, you cannot specify datetime data for the right side row value constructor element of the row value constructor of the IN predicate. For details about predefined input representations, see 6.3.3 Predefined character-string representations.

  3. A dynamic parameter cannot be specified alone in a row value constructor element of a row value constructor on the left of a IN predicate.

  4. When a dynamic parameter is specified alone as a row value constructor element of the row value constructor on the right side of an IN predicate, the data type of the dynamic parameter is assumed to be the data type of the row value constructor element of the row value constructor on the left side of the IN predicate with which it is to be compared.

  5. The right side of the IN predicate can only specify the value of the row value constructor element of the row value constructor.

  6. The row value constructor element of the row value constructor to the left of the IN predicate cannot specify array type data or STRUCT type data.

  7. The following value expressions cannot be specified in the row value constructor elements of the row value constructor to the left of the IN predicate

    • Set function

    • Scalar subquery

    • Array element reference

  8. An IN predicate with a row value constructor can specify either of the following.

    • WHERE clause

    • WHERE search condition in a DELETE or UPDATE statement

  9. An IN 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 5, 2011.

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

The underlined portion indicates the IN predicate.

Example 2

Retrieve the customer ID (USERID), product code (PUR-CODE), and quantity purchased (PUR-NUM) from the sales history table (SALESLIST). At this time, skip retrieval for customers whose customer IDs (USERID) are U00212 and U00358.

SELECT "USERID","PUR-CODE","PUR-NUM"
    FROM "SALESLIST"
       WHERE "USERID" NOT IN ('U00212','U00358')

The underlined portion indicates the IN predicate.

Example 3

From the customer table (USERSLIST) and sales history table (SALESLIST), retrieve information on customers who purchased product code (PUR-CODE) P001.

SELECT * FROM "USERSLIST"
    WHERE "USERID" IN (SELECT "USERID" FROM "SALESLIST"
                            WHERE "PUR-CODE"='P001')

The underlined portion indicates the IN predicate.

Example 4

Rows that match the following conditions are retrieved from the sales history table (SALESLIST).

  • Customer ID (USERID) is U001 and product code (PUR-CODE) is P001

  • Customer ID (USERID) is U002 and product code (PUR-CODE) is P001

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

The underlined portion is the specification of the IN predicate that specifies the row value constructors.