Collection Subquery: (1) |---------MULTISET--(--+-subquery----------------------+--)-----| '-SELECT ITEM--singleton_select-'
Element | Description | Restrictions | Syntax |
---|---|---|---|
singleton
_select |
Subquery returning exactly one row | Subquery cannot repeat the SELECT keyword, nor include the ORDER BY clause | SELECT, p. SELECT |
subquery | Embedded query | Cannot contain the ORDER BY clause | SELECT, p. SELECT |
The MULTISET and SELECT ITEM keywords have the following significance:
You can use a collection subquery in the following contexts:
The following restrictions apply to a collection subquery:
A collection subquery returns a multiset of unnamed ROW data types. The fields of this ROW type are elements in the projection list of the subquery. Examples that follow access the tables and the ROW types that these statements define:
CREATE ROW TYPE rt1 (a INT); CREATE ROW TYPE rt2 (x int, y rt1); CREATE TABLE tab1 (col1 rt1, col2 rt2); CREATE TABLE tab2 OF TYPE rt1; CREATE TABLE tab3 (a ROW(x INT));
The following examples of collection subqueries return the MULTISET collections that are listed to the right of the subquery.
Collection Subquery | Resulting Collections |
---|---|
MULTISET (SELECT * FROM tab1)... | MULTISET(ROW(col1 rt1, col2 rt2)) |
MULTISET (SELECT col2.y FROM tab1)... | MULTISET(ROW(y rt1)) |
MULTISET (SELECT * FROM tab2)... | MULTISET(ROW(a int)) |
MULTISET(SELECT p FROM tab2 p)... | MULTISET(ROW(p rt1)) |
MULTISET (SELECT * FROM tab3)... | MULTISET(ROW(a ROW(x int))) |
The following is another collection subquery:
SELECT f(MULTISET(SELECT * FROM tab1 WHERE tab1.x = t.y)) FROM t WHERE t.name = 'john doe';
The following collection subquery includes the UNION operator:
SELECT f(MULTISET(SELECT id FROM tab1 UNION SELECT id FROM tab2 WHERE tab2.id2 = tab3.id3)) FROM tab3;