SnowConvert AI - ANSI SQL - Subqueries¶
Description¶
A subquery is a query within another query. Subqueries in a FROM or WHERE clause are used to provide data that will be used to limit or compare/evaluate the data returned by the containing query. (Snowflake subqueries documentation).
Subqueries can be correlated/uncorrelated as well as scalar/non-scalar.
Correlated subqueries reference columns from the outer query. In Snowflake, correlated subqueries execute for each row in the query. On the other hand, Uncorrelated subqueries do not reference the outer query and are executed once for the entire query.
Scalar subqueries return a single value as result, otherwise the subquery is non-scalar.
The following patterns are based on these categories.
Sample Source Patterns¶
Setup data¶
Teradata¶
Snowflake¶
Non-scalar subqueries¶
Non-scalar subqueries specified inside subquery operators (ANY/ALL/IN/EXISTS) are supported.
Non-scalar subqueries used as derived tables are also supported.
Teradata¶
Result¶
Snowflake¶
Results¶
Known Issues¶
1. Subqueries with FETCH first that are not uncorrelated scalar
Oracle allows using the FETCH clause in subqueries, Snowflake only allows using this clause if the subquery is uncorrelated scalar, otherwise an exception will be generated.
SnowConvert AI will mark any inalid usage of FETCH in subqueries with SSC-EWI-0108
Oracle:
Snowflake: