There has been substantial work on “unnesting” complex
SQL queries containing nested subqueries by translating them
into single block SQL queries when certain syntactic
restrictions are satisfied. Another direction that has
been pursued in optimizing nested subqueries is reducing the
number of invocations and batching invocation of inner
subqueries by semi-join like techniques. Likewise, the
problem of flattening queries containing views has been a
topic of interest. The case where participating views are SPJ
queries is well understood. The problem is more complex
when one or more of the views contain aggregation.
Naturally, this problem is closely related to the problem of
commuting group-by and join operators. However,
commuting group-by and join is applicable in the context of
single block SQL queries as well. An overview of the
field appears in a recent paper.