Re: Bug in sub-query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Veeranjaneya Vara Prasad Peddireddy <varaprasad(dot)peddireddy(at)valuelabs(dot)com>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Bug in sub-query
Date: 2025-12-12 15:26:12
Message-ID: 988549.1765553172@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Veeranjaneya Vara Prasad Peddireddy <varaprasad(dot)peddireddy(at)valuelabs(dot)com> writes:
> --query 2:
> select * from admin.emp where location_id_location=108
> and sno in (select dim_id from admin.emp_dimension where location_id_location=108)
> --Note2: it returned two records and no error given.
> --But if you observe above query 2; the sub-query is not correct. In sub-query; location_id_location is not present in table admin.emp_dimension. But same sub-query failed in query 1.

This is not a bug, it is behavior required by the SQL standard.
If location_id_location is not found among the columns available
from the sub-query's FROM clause, it will be treated as an
"outer reference" to columns available from the outer query.

The usual way to avoid falling into this trap is to always
relation-qualify column references, especially in sub-queries.

regards, tom lane

In response to

  • Bug in sub-query at 2025-12-12 10:59:25 from Veeranjaneya Vara Prasad Peddireddy

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2025-12-12 15:39:55 Re: BUG #19352: SQL Error messages do not include schema name along with table or data object name.
Previous Message Álvaro Herrera 2025-12-12 15:10:48 Re: BUG #19352: SQL Error messages do not include schema name along with table or data object name.