what standard say ...

Started by Vadim B. Mikheevalmost 28 years ago6 messages
#1Vadim B. Mikheev
vadim@sable.krasnoyarsk.su

vac=> \d test

Table    = test
+----------------------------------+----------------------------------+-------+
|              Field               |              Type                | Length|
+----------------------------------+----------------------------------+-------+
| x                                | int4                             |     4 |
| y                                | int4                             |     4 |
+----------------------------------+----------------------------------+-------+
vac=> select count(*) from test where exists (select t1.y from test t1 where t1.y = x);
                                                                                    ^
Is this correlated subquery or not ?
(Note, that I don't use x with t1. prefix here)
With current parser this works as un-correlated subquery...
Is this Ok and I have to re-write query as

vac=> select count(*) from test t2 where exists
^^
(select t1.y from test t1 where t1.y = t2.x);
^^^
to get correlated one ?

Vadim

#2Thomas G. Lockhart
lockhart@alumni.caltech.edu
In reply to: Vadim B. Mikheev (#1)
Re: [HACKERS] what standard say ...

Vadim B. Mikheev wrote:

vac=> \d test

Table    = test
+----------------------------------+----------------------------------+-------+
|              Field               |              Type                | Length|
+----------------------------------+----------------------------------+-------+
| x                                | int4                             |     4 |
| y                                | int4                             |     4 |
+----------------------------------+----------------------------------+-------+
vac=> select count(*) from test where exists (select t1.y from test t1 where t1.y = x);
^
Is this correlated subquery or not ?
(Note, that I don't use x with t1. prefix here)
With current parser this works as un-correlated subquery...
Is this Ok and I have to re-write query as

vac=> select count(*) from test t2 where exists
^^
(select t1.y from test t1 where t1.y = t2.x);
^^^
to get correlated one ?

From "The SQL Standard", 3rd ed., Date and Darwen:

"... each unqualified column name is _implicitly_ qualified by a range variable name
defined (explicitly or implicitly) in the nearest applicable FROM clause." (the emphasis
is from the book, not me)

It goes on to recommend reading the standard for full understanding, but it is pretty
clear that your interpretation is correct; in the example above x is implicitly equivalent
to t1.x.

- Tom

#3Vadim B. Mikheev
vadim@sable.krasnoyarsk.su
In reply to: Vadim B. Mikheev (#1)
Re: [HACKERS] what standard say ...

Thomas G. Lockhart wrote:

vac=> select count(*) from test where exists (select t1.y from test t1 where t1.y = x);
^
Is this correlated subquery or not ?
(Note, that I don't use x with t1. prefix here)
With current parser this works as un-correlated subquery...

From "The SQL Standard", 3rd ed., Date and Darwen:

"... each unqualified column name is _implicitly_ qualified by a range variable name
defined (explicitly or implicitly) in the nearest applicable FROM clause." (the emphasis
is from the book, not me)

It goes on to recommend reading the standard for full understanding, but it is pretty
clear that your interpretation is correct; in the example above x is implicitly equivalent
to t1.x.

Ok. Nice to know that we are correct here :)

Vadim

#4Bruce Momjian
maillist@candle.pha.pa.us
In reply to: Vadim B. Mikheev (#1)
Re: [HACKERS] what standard say ...

vac=> \d test

Table    = test
+----------------------------------+----------------------------------+-------+
|              Field               |              Type                | Length|
+----------------------------------+----------------------------------+-------+
| x                                | int4                             |     4 |
| y                                | int4                             |     4 |
+----------------------------------+----------------------------------+-------+
vac=> select count(*) from test where exists (select t1.y from test t1 where t1.y = x);
^
Is this correlated subquery or not ?
(Note, that I don't use x with t1. prefix here)
With current parser this works as un-correlated subquery...
Is this Ok and I have to re-write query as

vac=> select count(*) from test t2 where exists
^^
(select t1.y from test t1 where t1.y = t2.x);
^^^
to get correlated one ?

Vadim

I am almost sure this is uncorrelated. If an unqualified varaiable
appears in a subquery, it matches the closest table it can find.

I am not sure about the standard, but logic would suggest this is the
way it should work.

And, of course, that is what the parser does.

--
Bruce Momjian
maillist@candle.pha.pa.us

#5Zeugswetter Andreas DBT
Andreas.Zeugswetter@telecom.at
In reply to: Bruce Momjian (#4)
Re: what standard say ...

vac=> select count(*) from test where exists (select t1.y from test t1

where t1.y = x);

^

Is this correlated subquery or not ?

No! Informix behaves in the same manner, I think it is ok.

Andreas

#6Zeugswetter Andreas DBT
Andreas.Zeugswetter@telecom.at
In reply to: Zeugswetter Andreas DBT (#5)
Re: what standard say ...

Is this Ok and I have to re-write query as
vac=> select count(*) from test t2 where exists
^^
(select t1.y from test t1 where t1.y = t2.x);
^^^
to get correlated one ?

Yes