Subselect with incorrect column not a syntax error?

Started by Mike Blackwellalmost 14 years ago4 messagesgeneral
Jump to latest
#1Mike Blackwell
mike.blackwell@rrd.com

Could someone please explain to me why the following select does not
result in a syntax error? (9.0.3)

begin;

create table x( c1 integer , c2 integer);
create table y( c3 integer, c4 integer);

select * from x where c2 in ( select c2 from y where c4 = 2 );

rollback;

Mike

#2Jack Christensen
jackc@hylesanderson.edu
In reply to: Mike Blackwell (#1)
Re: Subselect with incorrect column not a syntax error?

On 4/13/2012 11:39 AM, Mike Blackwell wrote:

Could someone please explain to me why the following select does not
result in a syntax error? (9.0.3)

begin;

create table x( c1 integer , c2 integer);
create table y( c3 integer, c4 integer);

select * from x where c2 in ( select c2 from y where c4 = 2 );

rollback;

Mike

Your subquery is correlated with the outer query. So the c2 in the
subquery is referring to table x.

--
Jack Christensen
jackc@hylesanderson.edu

#3Scott Marlowe
scott.marlowe@gmail.com
In reply to: Jack Christensen (#2)
Re: Subselect with incorrect column not a syntax error?

On Fri, Apr 13, 2012 at 11:59 AM, Jack Christensen
<jackc@hylesanderson.edu> wrote:

On 4/13/2012 11:39 AM, Mike Blackwell wrote:

Could someone please explain to me why the following select does not
result in a syntax error?  (9.0.3)

begin;

create table x( c1 integer , c2 integer);
create table y( c3 integer, c4 integer);

select * from x where c2 in ( select c2 from y where c4 = 2 );

rollback;

Mike

Your subquery is correlated with the outer query. So the c2 in the subquery
is referring to table x.

This is a good example of why one should always use a table alias
prefix when using subqueries.

#4Mike Blackwell
mike.blackwell@rrd.com
In reply to: Scott Marlowe (#3)
Re: Subselect with incorrect column not a syntax error?

Indeed.
__________________________________________________________________________________
Mike Blackwell | Technical Analyst, Distribution Services/Rollout
Management | RR Donnelley
1750 Wallace Ave | St Charles, IL 60174-3401
Office: 630.313.7818
Mike.Blackwell@rrd.com
http://www.rrdonnelley.com

Show quoted text

On Fri, Apr 13, 2012 at 13:34, Scott Marlowe <scott.marlowe@gmail.com> wrote:

On Fri, Apr 13, 2012 at 11:59 AM, Jack Christensen
<jackc@hylesanderson.edu> wrote:

On 4/13/2012 11:39 AM, Mike Blackwell wrote:

Could someone please explain to me why the following select does not
result in a syntax error?  (9.0.3)

begin;

create table x( c1 integer , c2 integer);
create table y( c3 integer, c4 integer);

select * from x where c2 in ( select c2 from y where c4 = 2 );

rollback;

Mike

Your subquery is correlated with the outer query. So the c2 in the subquery
is referring to table x.

This is a good example of why one should always use a table alias
prefix when using subqueries.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general