Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 995
Rens Verhage Avatar asked Jan 30 '26 17:01

Rens Verhage


1 Answers

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);
like image 160
Ihor Romanchenko Avatar answered Feb 02 '26 08:02

Ihor Romanchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!