March 16th, 2009

Correlated Subselects

Posted by admin in N. Subselects

A correlated subselect is a subselect that can not be evaluated without the use of the outer select, because it depends on the results of the outer select for its values.

The correlated subselect refers to entries in the FROM clause of the outer select. Therefore, references to columns in the correlated subselect must be qualified column names to explicitly specify their table of origin. Since subselects can be nested many levels deep, a correlated subselect may reference any level higher than its own level.

Execution starts with the outer select, and with each value returned, the inner correlated subselect is executed until processing of both the outer and inner selects is complete.

The subselect may reference the same table as the outer select, or it may reference a different table.

          Format:           SELECT outer_columns
                                   FROM outer_tables
                                 WHERE outer_value operator
                                            (SELECT inner_columns
                                               FROM inner_tables
                                             WHERE inner_column operator outer_column);

> DB2, Oracle, Postgre SQL, and SQL Server support correlated subselects in the WHERE and HAVING clauses.

> MySQL supports correlated subselects in the WHERE clause only.

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: