Oracle 5.0 Reference Manual page 1123

Table of Contents

Advertisement

AND cities_stores.store_type = stores.store_type));
The last example is a double-nested
a
NOT EXISTS
Stores"? But it is easier to say that a nested
13.2.9.7. Correlated Subqueries
A correlated subquery is a subquery that contains a reference to a table that also appears in the outer
query. For example:
SELECT * FROM t1
WHERE column1 = ANY (SELECT column1 FROM t2
Notice that the subquery contains a reference to a column of t1, even though the subquery's
clause does not mention a table t1. So, MySQL looks outside the subquery, and finds
query.
Suppose that table
contains a row where
t2
column1 = ANY (SELECT column1 FROM t2)
clause within the subquery is
whole is FALSE.
Scoping rule: MySQL evaluates from inside to outside. For example:
SELECT column1 FROM t1 AS x
WHERE x.column1 = (SELECT column1 FROM t2 AS x
WHERE x.column1 = (SELECT column1 FROM t3
WHERE x.column2 = t3.column1));
In this statement,
renames t2. It is not a column in table
AS x ...
an outer query that is farther out.
For subqueries in
select list.
For certain cases, a correlated subquery is optimized. For example:
val
IN (SELECT
Otherwise, they are inefficient and likely to be slow. Rewriting the query as a join might improve
performance.
Aggregate functions in correlated subqueries may contain outer references, provided the function
contains nothing but outer references, and provided the function is not contained in another function or
expression.
13.2.9.8. Subqueries in the
Subqueries are legal in a
SELECT ... FROM (subquery) [AS]
The
[AS] name
columns in the
For the sake of illustration, assume that you have this table:
CREATE TABLE t1 (s1 INT, s2 CHAR(5), s3 FLOAT);
Subquery Syntax
NOT EXISTS
clause. Formally, it answers the question "does a city exist with a store that is not in
WHERE t2.column2 = t1.column2);
contains a row where
t1
column1 = 5
(because
FALSE
must be a column in table
x.column2
or
HAVING
ORDER BY
key_val
FROM
tbl_name
Clause
FROM
statement's
SELECT
name
clause is mandatory, because every table in a
select list must have unique names.
subquery
query. That is, it has a
answers the question "is
NOT EXISTS
and
column1 = 5
and
7. The simple expression
column2 =
would be TRUE, but in this example, the
is not equal to (5,7)), so the expression as a
(5,6)
because
t2
because
t1
clauses, MySQL also looks for column names in the outer
WHERE correlated_condition)
clause. The actual syntax is:
FROM
...
1103
NOT EXISTS
x TRUE
6; meanwhile, table
column2 =
... WHERE
SELECT column1 FROM t2
SELECT column1 FROM t1 ...
clause must have a name. Any
FROM
clause within
for all y?"
FROM
in the outer
t1
WHERE
is

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents