Assume the two tables:
Table A: A1, A2, A_Other
Table B: B1, B2, B_Other
In the following examples, is something
is a condition checked against a fixed value, e.g. = 'ABC'
or < 45
.
I wrote a query like 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 meant to write was (2):
Select * from A
Where A1 IN (
Select Distinct B1 from B
Where B2 is something
)
And A2 is something;
Strangely, both queries returned the same result. When looking at the explain plan of query 1, it looked like when the subquery was executed, because the condition A2 is something
was not applicable to the subquery, it was deferred for use as a filter on the main query results.
I would normally expect query 1 to fail because the subquery by 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 this is not the case, and Postgres defers inapplicable subquery conditions to the main query.
Is this standard behaviour or a Postgres anomaly? Where is this documented, and what is this feature called?
Also, I find that if I add a column A2
in table B
, only query 2 works as originally intended. In this case the reference A2
in query 2 would still refer to A.A2
, but the reference in query 1 would refer to the new column B.A2
because it is now applicable directly in the subquery.