Contents Aster SQL and Function Reference


Add to my manuals
228 Pages

advertisement

Contents Aster SQL and Function Reference | Manualzz

Aster Data proprietary and confidential Subquery SQL Expressions

NULLIF

NULLIF(value1, value2)

The

NULLIF

function returns a null value if

value1

and

value2

are equal; otherwise it returns

value1

. This can be used to perform the inverse operation of the

COALESCE

example given above:

SELECT NULLIF(value, '(none)') ...

If

value1

is (none), return a null, otherwise return

value1

.

GREATEST

and

LEAST

GREATEST(value [, ...])

LEAST(value [, ...])

The

GREATEST

and

LEAST

functions select the largest or smallest value from a list of any number of expressions. The expressions must all be convertible to a common datatype, which will be the type of the result.

NULL

values in the list are ignored. The result will be

NULL

only if all the expressions evaluate to

NULL

.

Note that

GREATEST

and

LEAST

are not in the SQL standard, but are a common extension.

Subquery SQL Expressions

This section describes the SQL-compliant subquery expressions available in Aster Database. All of the expression forms documented in this section return Boolean (true/false) results.

EXISTS

EXISTS (subquery)

The argument of

EXISTS

is an arbitrary

SELECT

statement, or subquery. The subquery is evaluated to determine whether it returns any rows. If it returns at least one row, the result of

EXISTS

is true

; if the subquery returns no rows, the result of

EXISTS

is false

.

The subquery will generally only be executed far enough to determine whether at least one row is returned, not all the way to completion. It is unwise to write a subquery that has any side effects; whether the side effects occur or not may be difficult to predict.

Since the result depends only on whether any rows are returned, and not on the contents of those rows, the output list of the subquery is normally uninteresting. A common coding convention is to write all

EXISTS

tests in the form

EXISTS(SELECT 1 WHERE ...)

. There are exceptions to this rule however, such as subqueries that use

INTERSECT

.

IN

expression IN (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of

IN

is true

if any equal subquery row is found. The result is false

if no equal row is found

(including the special case where the subquery returns no rows).

December 14, 2011 Functions and Operators V--133

Subquery SQL Expressions Aster Data proprietary and confidential

Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the

IN

construct will be null, not false

. This is in accordance with SQL’s normal rules for Boolean combinations of null values.

As with

EXISTS

, it's unwise to assume that the subquery will be evaluated completely.

Note on SQL compliance: There is a small difference between a fully SQL-compliant implementation of IN/NOT IN and the Aster Database implementation. We can illustrate this with the example expression, “

1 IN (<subquery>)

”. Three cases are possible:

Case 1: The subquery output contains 1. In this case both SQL-compliant implementations and Aster Database return true.

Case 2: The subquery output does not contain 1, and does not contain a NULL. In this case, both SQL-compliant implementations and Aster Database return false.

Case 3: The subquery output does not contain 1, but it does contain a NULL. In this case, an

SQL-compliant implementation returns NULL, but the Aster Database implementation returns false.

NOT IN

expression NOT IN (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of

NOT IN

is true

if only unequal subquery rows are found (including the special case where the subquery returns no rows). The result is false

if any equal row is found.

Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the

NOT IN

construct will be null, not true

.

This is in accordance with SQL’s normal rules for Boolean combinations of null values.

As with

EXISTS

, it's unwise to assume that the subquery will be evaluated completely.

See also the Note on SQL Compliance in the section above explaining “IN”.

ANY/SOME

expression operator ANY (subquery) expression operator SOME (subquery)

Used as an equality, ANY evaluates to TRUE if any one of a set of comparisons is TRUE. The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given operator, which must yield a Boolean result. The result of

ANY

is true

if any true

result is obtained. The result is false

if no true

result is found (including the special case where the subquery returns no rows).

SOME

is a synonym for

ANY

.

IN

is equivalent to

= ANY

.

Note that if there are no successes and at least one right-hand row yields null for the operator's result, the result of the

ANY

construct will be null, not false

. This is in accordance with SQL’s normal rules for Boolean combinations of null values.

As with

EXISTS

, it's unwise to assume that the subquery will be evaluated completely.

V--134 Database SQL and Function Reference, version 4.6.2

aster data

Aster Data proprietary and confidential Subquery SQL Expressions

ALL

expression operator ALL (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given operator, which must yield a Boolean result. The result of

ALL

is true

if all rows yield true

(including the special case where the subquery returns no rows). The result is false

if any false

result is found. The result is

NULL

if the comparison does not return false

for any row, and it returns

NULL

for at least one row.

NOT IN

is equivalent to

<> ALL

.

As with

EXISTS

, it's unwise to assume that the subquery will be evaluated completely.

December 14, 2011 Functions and Operators V--135

Subquery SQL Expressions Aster Data proprietary and confidential

V--136 Database SQL and Function Reference, version 4.6.2

aster data

advertisement

Was this manual useful for you? Yes No
Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Download PDF

advertisement

Table of contents