Assume two tables:
Table A: A1, A2, A_Other Table B: B1, B2, B_Other
In the following examples, is something is a condition checked for a fixed value, for example. = 'ABC' or < 45 .
I wrote a query similar to the following (1) :
Select * from A Where A1 IN ( Select Distinct B1 from B Where B2 is something And A2 is something );
What I really wanted to write was (2) :
Select * from A Where A1 IN ( Select Distinct B1 from B Where B2 is something ) And A2 is something;
Strange, both queries return the same result. If you look at the plan for explaining query 1 , it looked like when the subquery was executed, since condition A2 is something does not apply to the subquery, it was delayed for use as a filter by the main results of the query.
I usually expected query 1 to fail because the subquery itself would fail:
Select Distinct B1 from B Where B2 is something And A2 is something; --- ERROR: column "A2" does not exist
But I find that this is not the case, and Postgres rejects the inapplicable subquery conditions for the main request.
Is this standard behavior or Postgres anomaly? Where is this documented and what is called this function?
In addition, I found that if I add column A2 to table B , only query 2 works as originally intended. In this case, the link A2 in query 2 will still refer to A.A2 , but the link in query 1 will refer to the new column B.A2 , because this is now directly applicable in the subquery.