in the Snowflake documentation it lists the types of sub-query that are supported Types Supported by Snowflake:
- Uncorrelated scalar subqueries in any place that a value expression can be used.
- Correlated scalar subqueries in WHERE clauses.
- EXISTS, ANY / ALL, and IN subqueries in WHERE clauses. These subqueries can be correlated or uncorrelated.
However, in the documentation about windows functions (as an example) it gives this SQL which runs without error:
select branch_id,
net_profit as store_profit,
(select sum(net_profit) from store_sales as s2 where s2.city = s1.city) as city_profit,
store_profit / city_profit * 100 as store_percentage_of_city_profit
from store_sales as s1
order by branch_id;
From my understanding:
- city_profit is defined by a sub-query and that sub-query is correlated (it is referencing S1)
- It is not covered by any of the 3 rules regarding allowable sub-queries (it is not uncorrelated, it is not in a WHERE clause and it is not "EXISTS, ANY / ALL, and IN subqueries in WHERE clauses"
Therefore the query should be invalid but as it isn't I am obviously missing something. What have I misunderstood?