Re: why isn't this subquery wrong?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: jonathan vanasco <postgres(at)2xlp(dot)com>, pgsql-general general <pgsql-general(at)postgresql(dot)org>
Subject: Re: why isn't this subquery wrong?
Date: 2017-04-21 00:04:32
Message-ID: CAKFQuwYhLMSzcJtHx=5vV5uqUwdc2nan_A_nKkBZRMdtFe4khw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 20, 2017 at 3:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> jonathan vanasco <postgres(at)2xlp(dot)com> writes:
> > Can anyone explain to me why the following is valid (running 9.6) ?
>
> > SELECT foo_id
> > FROM example_a__data
> > WHERE foo_id IN (SELECT bar_id FROM example_a__rollup)
> > ;
>
> Per the SQL standard, bar_id is interpreted as an "outer reference"
> to example_a__data.bar_id. This trips people up all the time, but
> (a) it's required by spec and (b) there are cases where it's really
> hard to do what you want without an outer reference.
>
> Cautious SQL programmers qualify all references inside sub-selects
> to avoid getting caught by this accidentally.
>

​I added an FAQ entry to the wiki​ for this question:

https://wiki.postgresql.org/wiki/FAQ#Why_doesn.27t_PostgreSQL_report_a_column_not_found_error_when_using_the_wrong_name_in_a_subquery.3F

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Akshay Joshi 2017-04-21 06:27:49 pgAdmin4 needs information of v10 SCRAM authentication
Previous Message David G. Johnston 2017-04-20 23:16:56 Re: why isn't this subquery wrong?