informix
Informix Guide to SQL: Syntax
Segments

Condition

Use a condition to test data to determine whether it meets certain qualifications. Use the Condition segment wherever you see a reference to a condition in a syntax diagram.

Syntax

Usage

A condition is a collection of one or more search conditions, optionally connected by the logical operators AND or OR. Search conditions fall into the following categories:

Restrictions on a Condition

A condition can contain only an aggregate function if it is used in the HAVING clause of a SELECT statement or the HAVING clause of a subquery. You cannot use an aggregate function in a comparison condition that is part of a WHERE clause in a DELETE, SELECT, or UPDATE statement unless the aggregate is on a correlated column that originates from a parent query and the WHERE clause is within a subquery that is within a HAVING clause.

NOT Operator Option

If you preface a condition with the keyword NOT, the test is true only if the condition that NOT qualifies is false. If the condition that NOT qualifies is unknown (uses a null in the determination), the NOT operator has no effect. The following truth table shows the effect of NOT. The letter T represents a true condition, F represents a false condition, and a question mark (?) represents an unknown condition. Unknown values occur when part of an expression that uses an arithmetic operator is null.

Comparison Conditions (Boolean Expressions)

Five kinds of comparison conditions exist: Relational Operator, BETWEEN, IN, IS NULL, and LIKE and MATCHES. Comparison conditions are often called Boolean expressions because they evaluate to a simple true or false result. Their syntax is summarized in the following diagram and explained in detail after the diagram.

Element Purpose Restrictions Syntax
char A single ASCII character that is to be used as the escape character within the quoted string in a LIKE or MATCHES condition See ESCAPE with LIKE and ESCAPE with MATCHES. Quoted String, p. 4-260

Refer to the following sections for more information on the use of the different types of comparison conditions:

For a discussion of the different types of comparison conditions in the context of the SELECT statement, see Using a Condition in the WHERE Clause.

Warning: When you specify a date value in a comparison condition, make sure to specify 4 digits instead of 2 digits for the year. When you specify a 4-digit year, the DBCENTURY environment variable has no effect on how the database server interprets the comparison condition. When you specify a 2-digit year, the DBCENTURY environment variable can affect how the database server interprets the comparison condition, so the comparison condition might not work as you intended. For more information on the DBCENTURY environment variable, see the "Informix Guide to SQL: Reference."

Column Name

Element Purpose Restrictions Syntax
alias Temporary alternative name for a table or view within the scope of a SELECT statement You must have defined the alias in the FROM clause of the SELECT statement. Identifier, p. 4-205
column Name of a column that is used in an IS NULL condition or in a LIKE or MATCHES condition. See IS NULL Condition and LIKE and MATCHES Condition for more information on the meaning of column name in these conditions. The column must exist in the specified table. Identifier, p. 4-205
field Name of the field that you want to compare in the row column The field must be a component of the row type that row-column name or field name (for nested rows) specifies. Identifier, p. 4-205
row_column Name of the row column that you specify The data type of the column must be a named row type or an unnamed row type. Identifier, p. 4-205
synonym Name of a view The synonym must exist within the database. Database Object Name, p. 4-50
table Name of a table The table must exist within the database. Database Object Name, p. 4-50
view Name of a synonym The view must exist within the database. Database Object Name, p.4-50

Quotation Marks in Conditions

When you compare a column expression with a constant expression in any type of comparison condition, observe the following rules:

The following example shows the correct use of quotation marks in comparison conditions. The ship_instruct column has a character data type. The order_date column has a date data type. The ship_weight column has a numeric data type.

Relational-Operator Condition

Some relational-operator conditions are shown in the following examples:

If either expression is null for a row, the condition evaluates to false. For example, if the paid_date column has a null, you cannot use either of the following statements to retrieve that row:

An IS NULL condition finds a null value, as shown in the following example. The IS NULL condition is explained fully in IS NULL Condition on page 4-36.

BETWEEN Condition

For a BETWEEN test to be true, the value of the expression on the left of the BETWEEN keyword must be in the inclusive range of the values of the two expressions on the right of the BETWEEN keyword. Null values do not satisfy the condition. You cannot use NULL for either expression that defines the range.

Some BETWEEN conditions are shown in the following examples:

IN Condition

The IN condition is satisfied when the expression to the left of the word IN is included in the list of items.

Element Purpose Restrictions Syntax
collection_col Name of a collection column that is used in an IN condition The column must exist in the specified table. Identifier, p. 4-205

The NOT option produces a search condition that is satisfied when the expression is not in the list of items. Null values do not satisfy the condition.

The following examples show some IN conditions:

In ESQL/C, the built-in TODAY function is evaluated at execution time; the built-in CURRENT function is evaluated when a cursor opens or when the query executes, if it is a singleton SELECT statement.

The built-in USER function is case sensitive; it perceives minnie and Minnie as different values.

Using the IN Operator with Collection Data Types

You can use the IN operator to determine if an element is contained in a collection. The collection you search can be a simple or nested collection. In a nested collection type, the element type of the collection is also a collection type.

When you use the IN operator to search for an element in a collection, the expression to the left or right of the IN keyword cannot contain a BYTE or TEXT data type.

Suppose you create the following table that contains two collection columns:

The following partial examples show how you might use the IN operator for search conditions on the collection columns of the tab_coll table:

In general, when you use the IN operator on a collection data type, the database server checks whether the value on the left of the IN operator is an element in the set of values on the right of the in operator.

IS NULL Condition

The IS NULL condition is satisfied if the column contains a null value. If you use the IS NOT NULL option, the condition is satisfied when the column contains a value that is not null. The following example shows an IS NULL condition:

LIKE and MATCHES Condition

A LIKE or MATCHES condition tests for matching character strings. The condition is true, or satisfied, when either of the following tests is true:

You can use the single quote (') only with the quoted string to match a literal quote; you cannot use the ESCAPE clause. You can use the quote character as the escape character in matching any other pattern if you write it as ''''.

Important: You cannot specify a row-type column in a LIKE or MATCHES condition. A row-type column is a column that is defined on a named row type or unnamed row type. NOT Option

The NOT option makes the search condition successful when the column on the left has a value that is not null and does not match the pattern that the quoted string specifies. For example, the following conditions exclude all rows that begin with the characters Baxter in the lname column:

LIKE Option

If you use the keyword LIKE, you can use the following wildcard characters in the quoted string.

Wildcard Purpose
% Matches zero or more characters
_ Matches any single character
\ Removes the special significance of the next character (used to match % or _ by writing \% or \_)

Using the backslash (\) as an escape character is an Informix extension to ANSI-compliant SQL.

In an ANSI-compliant database, you can only use an escape character to escape a percent sign (%), an underscore (_), or the escape character itself.

The following condition tests for the string tennis, alone or in a longer string, such as tennis ball or table tennis paddle:

The following condition tests for all descriptions that contain an underscore. The backslash (\) is necessary because the underscore (_) is a wildcard character.

The LIKE operator has an associated operator function called like(). You can define a like() function to handle your own user-defined data types. For more information, see the Extending Informix Dynamic Server 2000 manual.

MATCHES Option

If you use the keyword MATCHES, you can use the following wildcard characters in the quoted string.

Wildcard Purpose
* Matches zero or more characters.
? Matches any single character.
[...] Match any\r of the enclosed characters, including character ranges as in [a-z]. Characters inside the square brackets cannot be escaped.
^ As the first character within the square brackets matches any character that is not listed. Hence [^abc] matches any character that is not a, b, or c.
\ Removes the special significance of the next character (used to match * or ? by writing \* or \?).

The following condition tests for the string tennis, alone or in a longer string, such as tennis ball or table tennis paddle:

The following condition is true for the names Frank and frank:

The following condition is true for any name that begins with either F or f:

The following condition is true for any name that ends with the letters a, b, c, or d:

The MATCHES operator has an associated operator function called matches(). You can define a matches() function to handle your own user-defined data types. For more information, see the Extending Informix Dynamic Server 2000 manual.

ESCAPE with LIKE

The ESCAPE clause lets you include an underscore (_) or a percent sign (%) in the quoted string and avoid having them be interpreted as wildcards. If you choose to use z as the escape character, the characters z_ in a string stand for the underscore character (_). Similarly, the characters z% represent the percent sign (%). Finally, the characters zz in the string stand for the single character z. The following statement retrieves rows from the customer table in which the company column includes the underscore character:

You can also use a single-character host variable as an escape character. The following statement shows the use of a host variable as an escape character:

ESCAPE with MATCHES

The ESCAPE clause lets you include a question mark (?), an asterisk (*), and a left or right square bracket ([]) in the quoted string and avoid having them be interpreted as wildcards. If you choose to use z as the escape character, the characters z? in a string stand for the question mark (?). Similarly, the characters z* stand for the asterisk (*). Finally, the characters zz in the string stand for the single character z.

The following example retrieves rows from the customer table in which the value of the company column includes the question mark (?):

Stand-Alone Condition

A stand-alone condition can be any expression that is not explicitly listed in the syntax for the comparison condition. Such an expression is valid only if its result is of the Boolean type. For example, the following example returns a value of the Boolean type:

Condition with Subquery

You can use a SELECT statement within a condition; this combination is called a subquery. You can use a subquery in a SELECT statement to perform the following functions:

The subquery can depend on the current row that the outer SELECT statement is evaluating; in this case, the subquery is a correlated subquery.

The kinds of subquery conditions are shown in the following sections with their syntax. For a discussion of the different kinds of subquery conditions in the context of the SELECT statement, see Using a Condition in the WHERE Clause.

A subquery can return a single value, no value, or a set of values depending on the context in which it is used. If a subquery returns a value, it must select only a single column. If the subquery simply checks whether a row (or rows) exists, it can select any number of rows and columns. A subquery cannot contain BYTE or TEXT data types, nor can it contain an ORDER BY clause. For a complete description of SELECT syntax and usage, see SELECT.

IN Subquery

Element Purpose Restrictions Syntax
subquery Embedded query The subquery cannot contain either the FIRST or the ORDER BY clause. SELECT, p. 2-634

An IN subquery condition is true if the value of the expression matches one or more of the values that the subquery selects. The subquery must return only one column, but it can return more than one row. The keyword IN is equivalent to the =ANY sequence. The keywords NOT IN are equivalent to the !=ALL sequence. See ALL, ANY, SOME Subquery.

The following example of an IN subquery finds the order numbers for orders that do not include baseball gloves (stock_num = 1):

Because the IN subquery tests for the presence of rows, duplicate rows in the subquery results do not affect the results of the main query. Therefore, you can put the UNIQUE or DISTINCT keyword into the subquery with no effect on the query results, although eliminating testing duplicates can reduce the time needed for running the query.

EXISTS Subquery

Element Purpose Restrictions Syntax
subquery Embedded query The subquery cannot contain either the FIRST or the ORDER BY clause. SELECT, p. 2-634

An EXISTS subquery condition evaluates to true if the subquery returns a row. With an EXISTS subquery, one or more columns can be returned. The subquery always contains a reference to a column of the table in the main query. If you use an aggregate function in an EXISTS subquery, at least one row is always returned.

The following example of a SELECT statement with an EXISTS subquery returns the stock number and manufacturer code for every item that has never been ordered (and is therefore not listed in the items table). You can appropriately use an EXISTS subquery in this SELECT statement because you use the subquery to test both stock_num and manu_code in items.

The preceding example works equally well if you use SELECT * in the subquery in place of the column names because the existence of the whole row is tested; specific column values are not tested.

ALL, ANY, SOME Subquery

Element Purpose Restrictions Syntax
subquery Embedded query The subquery cannot contain either the FIRST or the ORDER BY clause. SELECT, p. 2-634

You use the ALL, ANY, and SOME keywords to specify what makes the search condition true or false. A search condition that is true when the ANY keyword is used might not be true when the ALL keyword is used, and vice versa.

Using the ALL Keyword

The ALL keyword denotes that the search condition is true if the comparison is true for every value that the subquery returns. If the subquery returns no value, the condition is true.

In the following example of the ALL subquery, the first condition tests whether each total_price is greater than the total price of every item in order number 1023. The second condition uses the MAX aggregate function to produce the same results.

Using the NOT keyword with an ALL subquery tests whether an expression is not true for at least one element returned by the subquery. For example, the following condition is true when the expression total_price is not greater than all the selected values. That is, it is true when total_price is not greater than the highest total price in order number 1023.

Using the ANY or SOME Keywords

The ANY keyword denotes that the search condition is true if the comparison is true for at least one of the values that is returned. If the subquery returns no value, the search condition is false. The SOME keyword is an alias for ANY.

The following conditions are true when the total price is greater than the total price of at least one of the items in order number 1023. The first condition uses the ANY keyword; the second uses the MIN aggregate function.

Using the NOT keyword with an ANY subquery tests whether an expression is not true for all elements returned by the subquery. For example, the following condition is true when the expression total_price is not greater than any selected value. That is, it is true when total_price is greater than none of the total prices in order number 1023.

Omitting the ANY, ALL, or SOME Keywords

You can omit the keywords ANY, ALL, or SOME in a subquery if you know that the subquery will return exactly one value. If you omit the ANY, ALL, or SOME keywords, and the subquery returns more than one value, you receive an error. The subquery in the following example returns only one row because it uses an aggregate function:

Conditions with AND or OR

You can combine simple conditions with the logical operators AND or OR to form complex conditions. The following SELECT statements contain examples of complex conditions in their WHERE clauses:

The following truth tables show the effect of the AND and OR operators.The letter T represents a true condition, F represents a false condition, and the question mark (?) represents an unknown value. Unknown values occur when part of an expression that uses a logical operator is null.

If the Boolean expression evaluates to UNKNOWN, the condition is not satisfied.

Consider the following example within a WHERE clause:

The row where order_num = 1023 is the row where ship_weight is null. Because ship_weight is null, ship_charge/ship_weight is also null; therefore, the truth value of ship_charge/ship_weight < 5 is UNKNOWN. Because order_num = 1023 is TRUE, the AND table states that the truth value of the entire condition is UNKNOWN. Consequently, that row is not chosen. If the condition used an OR in place of the AND, the condition would be true.

Related Information

For discussions of comparison conditions in the SELECT statement and of conditions with a subquery, see the Informix Guide to SQL: Tutorial.

For information on the GLS aspects of conditions, see the Informix Guide to GLS Functionality.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved