Consider the following Snowflake SQL query using CTEs:
WITH first_cte AS ( SELECT 1 AS num ), second_cte AS ( SELECT num + 1 AS num FROM first_cte ) SELECT * FROM second_cte;
What will be the output of this query?
WITH first_cte AS ( SELECT 1 AS num ), second_cte AS ( SELECT num + 1 AS num FROM first_cte ) SELECT * FROM second_cte;
Think about how the second CTE uses the first CTE's output.
The first CTE returns one row with num = 1. The second CTE adds 1 to that value, resulting in num = 2. The final SELECT returns this single row.
Which of the following Snowflake CTE definitions correctly implements a recursive query to generate numbers from 1 to 3?
WITH RECURSIVE numbers AS ( SELECT 1 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num < 3 ) SELECT * FROM numbers;
Which option matches this behavior?
WITH RECURSIVE numbers AS ( SELECT 1 AS num UNION ALL SELECT num + 1 FROM numbers WHERE num < 3 ) SELECT * FROM numbers;
Snowflake supports recursive CTEs. Check the termination condition num < 3.
This is a valid recursive CTE in Snowflake. The anchor member selects 1. The recursive member adds 1 repeatedly until num >= 3, generating rows for 1, 2, 3.
In Snowflake, which statement best describes how CTEs affect query performance?
Think about how Snowflake processes CTEs internally.
Snowflake typically inlines CTEs into the main query, meaning they are not materialized separately unless forced. This helps avoid unnecessary overhead and can improve performance.
Consider a Snowflake environment where a user has SELECT permission on a base table but not on a view built on top of it. If the user runs a query with a CTE referencing the base table, which is true about data access?
Consider how permissions apply to base tables versus views and CTEs.
CTEs are just query constructs referencing base tables. If the user has SELECT permission on the base table, they can access data through the CTE. Permissions on views do not affect this.
Given the following Snowflake SQL query:
WITH cte AS ( SELECT 10 AS val ) SELECT c1.val AS first_val, c2.val AS second_val FROM cte c1 JOIN cte c2 ON c1.val = c2.val;
What is the output of this query?
WITH cte AS (
SELECT 10 AS val
)
SELECT c1.val AS first_val, c2.val AS second_val
FROM cte c1
JOIN cte c2 ON c1.val = c2.val;Think about how the join works when both sides have one identical row.
Both c1 and c2 refer to the same single-row CTE with val = 10. The join condition matches this row to itself, producing one row with both columns equal to 10.