Re: why isn't this subquery wrong?

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: jonathan vanasco <postgres(at)2xlp(dot)com>
Cc: pgsql-general general <pgsql-general(at)postgresql(dot)org>
Subject: Re: why isn't this subquery wrong?
Date: 2017-04-20 23:02:22
Message-ID: CAEfWYyy5DaKqZCBhArEXJ1zhYr_EBViU97ivvO_Kq-pWgsKqhg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 20, 2017 at 3:56 PM, jonathan vanasco <postgres(at)2xlp(dot)com> wrote:

> thanks all!
>
> On Apr 20, 2017, at 6:42 PM, David G. Johnston wrote:
>
> ​Subqueries can see all columns of the parent. When the subquery actually
> uses one of them it is called a "correlated subquery".
>
>
> i thought a correlated subquery had to note that table/alias, not a raw
> column. I guess i've just been adhering to good form.
>
>
> On Apr 20, 2017, at 6:43 PM, Tom Lane wrote:
>
> Cautious SQL programmers qualify all references inside sub-selects to
> avoid getting caught by this accidentally.
>
>
> is there a syntax to qualify a reference to lock a subquery to the current
> scope (disable looking at the parents)? that's how I got caught on this by
> accident.
>

Like Tom said, "qualify all references":

...(SELECT example_a__rollup.bar_id FROM example_a__rollup)...

Or shortened with alises:

...(SELECT x.bar_id FROM example_a__rollup x)...

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2017-04-20 23:16:56 Re: why isn't this subquery wrong?
Previous Message jonathan vanasco 2017-04-20 22:56:14 Re: why isn't this subquery wrong?