TIL this is possible in the general case. Neat!
> SQL-99 allows for nested subqueries at nearly all places within a query.
From a user’s point of view, nested queries can greatly simplify the formulation of complex queries.
However, nested queries that are correlated with the outer queries frequently lead to dependent joins with nested loops evaluations and thus poor performance.
We present a generic approach for unnesting arbitrary SQL queries. As a result, the de-correlated queries allow for much simpler and much more efficient query evaluation.
https://btw-2015.informatik.uni-hamburg.de/res/proceedings/Hauptband/Wiss/Neumann-Unnesting_Arbitrary_Querie.pdf
Post #809
284