0
0
Snowflakecloud~20 mins

Common Table Expressions (CTEs) in Snowflake - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding CTE Execution Order

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?

Snowflake
WITH first_cte AS (
  SELECT 1 AS num
), second_cte AS (
  SELECT num + 1 AS num FROM first_cte
)
SELECT * FROM second_cte;
ASyntax error due to missing semicolon
BA single row with num = 1
CAn empty result set
DA single row with num = 2
Attempts:
2 left
💡 Hint

Think about how the second CTE uses the first CTE's output.

Configuration
intermediate
2:00remaining
CTE with Recursive Query Behavior

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?

Snowflake
WITH RECURSIVE numbers AS (
  SELECT 1 AS num
  UNION ALL
  SELECT num + 1 FROM numbers WHERE num < 3
)
SELECT * FROM numbers;
AGenerates rows with num = 1, 2, 3
BSyntax error: Snowflake does not support RECURSIVE keyword
CGenerates only num = 1
DInfinite loop causing query timeout
Attempts:
2 left
💡 Hint

Snowflake supports recursive CTEs. Check the termination condition num < 3.

Architecture
advanced
2:00remaining
CTE Impact on Query Performance

In Snowflake, which statement best describes how CTEs affect query performance?

ACTEs are always materialized as temporary tables, increasing query time
BCTEs disable query optimization, causing slower execution
CCTEs are inlined into the main query, so they do not always increase query time
DCTEs cause Snowflake to create permanent tables, which slows queries
Attempts:
2 left
💡 Hint

Think about how Snowflake processes CTEs internally.

security
advanced
2:00remaining
CTE and Data Access Control

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?

ACTEs bypass Snowflake's access control, so user sees all data
BUser can access data through the CTE because it references the base table directly
CUser cannot access data through the CTE if the base table is restricted
DUser must have explicit permission on the CTE to access data
Attempts:
2 left
💡 Hint

Consider how permissions apply to base tables versus views and CTEs.

service_behavior
expert
2:00remaining
CTE with Multiple References and Query Result

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?

Snowflake
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;
AOne row with first_val = 10 and second_val = 10
BTwo rows with first_val = 10 and second_val = 10
CEmpty result set due to join condition
DSyntax error due to aliasing
Attempts:
2 left
💡 Hint

Think about how the join works when both sides have one identical row.