Correlation names and Column name qualifiers in correlated references

A correlation name can be defined in the from clause of a query and in the first clause of an update or delete statement.

for example, the clause FROM X.MYTABLE Z establishes Z as a correlation name for X.MYTABLE.

With Z defined as correlation name of X.MYTABLE, only Z can be used to qualify reference to a column of that instance of X.MYTABLE in that SELECT statement.

If a correlation name is specified for a table, view, nickname or alias name, any qualified reference to a column of that table, view, nickname or alias must use the correlation name, rather than  the table, view, nickname or alias name.

Example


SELECT *
FROM employee e
WHERE employee.project = ’abc’ /* incorrect */

The qualified reference to PROJECT should instead use the correlation name, ″E″, as shown below:

SELECT *
FROM employee e
WHERE e.project = ’abc’

below is also valid, there is no qualified reference to PROJECT.

SELECT *
FROM employee e
WHERE project = ’abc’

A fullselect is a form of a query that may be used as a component of various SQL statement.
A fullselect used within a search condition of any statement is called a subquery.
A fullselect used to retrieve a single value as an expression within a statement is called a scalar fullselect or scalar subquery.
A fullselect used in the FROM clause of a query is called a nest table expression.

Subqueries in search conditions, scalar subqueries and nested table expression are referred to as subqueries through the remainder of this topic.

A subquery may includes subqueries of its own, and these may, in turn, include subqueries. Thus an SQL statement may contain a hierarchy of subqueries. Those elements of the hierarchy that contain subqueries are said to be at a higher level than the subqueries they contain.

Every element of the hierarchy contains one or more table designators. A subquery can reference not only the columns of the tables at its own level in the hierarchy, but also the columns of the tables identified previously in the hierarchy, back to the highest level of the hierarchy. A reference to a column of a table identified at a higher level is called a correlated reference.

For compatibility with existing standards for SQL, both qualified and unqualified column names are allowed as correlated reference. However, it is good practice to qualify all column references used in subqueries.

When a column name in a subquery is qualified, each level of hierarchy is searched, starting at the same query as the qualified column name appears and continuing to the higher levels of hierarchy until a table designator that matches the qualifier is found. Once found, it is verified that the table contains the given column. If the table is found at a higher level than the level containing column name,  then it is a correlated reference to the level where the table designator was found.

Newsletter Updates

Enter your email address below to subscribe to our newsletter