Rens Verhage
Rens Verhage

Reputation: 5857

PostgreSQL subquery with syntax error gives a valid result

What is happening here?

I got two tables, test1 and test2:

create table test1 (id1 int4 primary key);
create table test2 (id2 int4 primary key);

As expected, this query:

select id1 from test2;

produces a syntax error:

ERROR:  column "id1" does not exist
LINE 1: select id1 from test2;

However, when I try to execute this query:

select * from test1 where id1 in (select id1 from test2);

PostgreSQL doesn't complain, executes the query and gives me:

 id1
-----
(0 rows)

Is there any logic in this? Or should I file a bug report?

Upvotes: 3

Views: 622

Answers (1)

Ihor Romanchenko
Ihor Romanchenko

Reputation: 28641

Columns from outer select are visible in sub-select.

Your query is equivalent to:

select * 
from test1 
where test1.id1 in (select test1.id1 from test2);

Upvotes: 4

Related Questions