Bird
Raised Fist0
Snowflakecloud~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using a WITH clause in Snowflake SQL?
easy
A. To set session variables
B. To permanently store data in a new table
C. To create a user-defined function
D. To define a temporary named result set for use in a query

Solution

  1. Step 1: Understand the role of the WITH clause

    The WITH clause defines a Common Table Expression (CTE), which is a temporary named result set used within a query.
  2. Step 2: Differentiate from other SQL features

    Unlike permanent tables or functions, CTEs exist only during query execution and help organize complex queries.
  3. Final Answer:

    To define a temporary named result set for use in a query -> Option D
  4. Quick Check:

    WITH clause = temporary named result set [OK]
Hint: WITH means temporary named query part [OK]
Common Mistakes:
  • Thinking WITH creates permanent tables
  • Confusing WITH with functions or variables
  • Assuming WITH changes session settings
2. Which of the following is the correct syntax to start a CTE in Snowflake?
easy
A. CREATE CTE cte_name AS (SELECT * FROM table);
B. DEFINE cte_name SELECT * FROM table;
C. WITH cte_name AS (SELECT * FROM table)
D. BEGIN CTE cte_name SELECT * FROM table;

Solution

  1. Step 1: Recall the standard CTE syntax

    In Snowflake, a CTE starts with the keyword WITH followed by the CTE name and AS, then the query in parentheses.
  2. Step 2: Compare options to syntax

    WITH cte_name AS (SELECT * FROM table) matches this pattern exactly: WITH cte_name AS (SELECT * FROM table)
  3. Final Answer:

    WITH cte_name AS (SELECT * FROM table) -> Option C
  4. Quick Check:

    CTE syntax starts with WITH [OK]
Hint: CTE always starts with WITH keyword [OK]
Common Mistakes:
  • Using CREATE instead of WITH
  • Omitting AS keyword
  • Using BEGIN or DEFINE which are invalid here
3. Given the query:
WITH cte AS (SELECT 1 AS num UNION ALL SELECT 2) SELECT SUM(num) FROM cte;

What is the output of this query?
medium
A. 3
B. 2
C. 1
D. Error

Solution

  1. Step 1: Understand the CTE content

    The CTE named 'cte' selects two rows with values 1 and 2 under the column 'num'.
  2. Step 2: Calculate the SUM of 'num'

    The main query sums the values 1 and 2, resulting in 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    1 + 2 = 3 [OK]
Hint: Sum values inside CTE then add [OK]
Common Mistakes:
  • Thinking UNION ALL removes duplicates
  • Confusing SUM with COUNT
  • Expecting syntax error due to CTE
4. Identify the error in the following Snowflake query using a CTE:
WITH cte AS SELECT * FROM employees SELECT * FROM cte;
medium
A. Missing parentheses around the CTE query
B. CTE name cannot be 'cte'
C. CTE cannot be used in the same query
D. Missing AS keyword before SELECT in CTE definition

Solution

  1. Step 1: Review CTE syntax requirements

    A CTE query must be enclosed in parentheses after the AS keyword.
  2. Step 2: Check the given query

    The query lacks parentheses around the SELECT statement in the CTE definition.
  3. Final Answer:

    Missing parentheses around the CTE query -> Option A
  4. Quick Check:

    CTE query must be in parentheses [OK]
Hint: CTE query always inside parentheses [OK]
Common Mistakes:
  • Omitting parentheses around CTE query
  • Thinking CTE names are restricted
  • Believing CTEs can't be reused in same query
5. You want to calculate the average salary per department using a CTE named dept_salaries that selects employee salaries and departments. Which query correctly uses the CTE to get the average salary per department?
hard
A. WITH dept_salaries AS SELECT department, salary FROM employees SELECT department, AVG(salary) FROM dept_salaries GROUP BY department;
B. WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT department, AVG(salary) FROM dept_salaries GROUP BY department;
C. WITH dept_salaries (department, salary) AS (SELECT department, salary FROM employees) SELECT AVG(salary) FROM dept_salaries;
D. WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT AVG(salary) FROM employees GROUP BY department;

Solution

  1. Step 1: Verify correct CTE syntax and usage

    WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT department, AVG(salary) FROM dept_salaries GROUP BY department; correctly defines the CTE with parentheses and AS, then uses it to select department and average salary grouped by department.
  2. Step 2: Check aggregation and grouping

    WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT department, AVG(salary) FROM dept_salaries GROUP BY department; groups by department and calculates AVG(salary) from the CTE, which is the intended calculation.
  3. Final Answer:

    WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT department, AVG(salary) FROM dept_salaries GROUP BY department; -> Option B
  4. Quick Check:

    CTE with correct syntax and grouping = WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT department, AVG(salary) FROM dept_salaries GROUP BY department; [OK]
Hint: Use WITH ... AS (...) then GROUP BY correctly [OK]
Common Mistakes:
  • Omitting parentheses in CTE definition
  • Not grouping by department when aggregating
  • Using employees table instead of CTE in main query