Common Table Expressions (CTEs) in Snowflake - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Common Table Expressions (CTEs) in Snowflake, it's important to understand how the time to run queries grows as the data size increases.
We want to know how the number of operations changes when the input data gets bigger.
Analyze the time complexity of this CTE query sequence.
WITH recent_orders AS (
SELECT * FROM orders WHERE order_date >= DATEADD(day, -30, CURRENT_DATE())
),
customer_totals AS (
SELECT customer_id, SUM(amount) AS total_spent FROM recent_orders GROUP BY customer_id
)
SELECT * FROM customer_totals WHERE total_spent > 1000;
This query uses two CTEs: one to filter recent orders, and another to sum spending per customer, then filters customers by total spent.
Look at the main operations that happen repeatedly.
- Primary operation: Scanning the orders table and grouping by customer_id.
- How many times: The orders table is scanned once; grouping depends on number of customers in recent orders.
As the number of recent orders grows, the scan and grouping take more time.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 rows scanned and grouped |
| 100 | About 100 rows scanned and grouped |
| 1000 | About 1000 rows scanned and grouped |
Pattern observation: The work grows roughly in direct proportion to the number of recent orders.
Time Complexity: O(n)
This means the time to run the query grows linearly with the number of rows processed in the CTE.
[X] Wrong: "CTEs always run once and do not affect query time as data grows."
[OK] Correct: CTEs are like temporary views; their cost depends on the data size they process, so bigger data means more work.
Understanding how CTEs impact query time helps you design efficient data pipelines and write queries that scale well.
"What if the CTE included a join with another large table? How would the time complexity change?"
Practice
WITH clause in Snowflake SQL?Solution
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.Step 2: Differentiate from other SQL features
Unlike permanent tables or functions, CTEs exist only during query execution and help organize complex queries.Final Answer:
To define a temporary named result set for use in a query -> Option DQuick Check:
WITH clause = temporary named result set [OK]
- Thinking WITH creates permanent tables
- Confusing WITH with functions or variables
- Assuming WITH changes session settings
Solution
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.Step 2: Compare options to syntax
WITH cte_name AS (SELECT * FROM table) matches this pattern exactly: WITH cte_name AS (SELECT * FROM table)Final Answer:
WITH cte_name AS (SELECT * FROM table) -> Option CQuick Check:
CTE syntax starts with WITH [OK]
- Using CREATE instead of WITH
- Omitting AS keyword
- Using BEGIN or DEFINE which are invalid here
WITH cte AS (SELECT 1 AS num UNION ALL SELECT 2) SELECT SUM(num) FROM cte;
What is the output of this query?
Solution
Step 1: Understand the CTE content
The CTE named 'cte' selects two rows with values 1 and 2 under the column 'num'.Step 2: Calculate the SUM of 'num'
The main query sums the values 1 and 2, resulting in 3.Final Answer:
3 -> Option AQuick Check:
1 + 2 = 3 [OK]
- Thinking UNION ALL removes duplicates
- Confusing SUM with COUNT
- Expecting syntax error due to CTE
WITH cte AS SELECT * FROM employees SELECT * FROM cte;
Solution
Step 1: Review CTE syntax requirements
A CTE query must be enclosed in parentheses after the AS keyword.Step 2: Check the given query
The query lacks parentheses around the SELECT statement in the CTE definition.Final Answer:
Missing parentheses around the CTE query -> Option AQuick Check:
CTE query must be in parentheses [OK]
- Omitting parentheses around CTE query
- Thinking CTE names are restricted
- Believing CTEs can't be reused in same query
dept_salaries that selects employee salaries and departments. Which query correctly uses the CTE to get the average salary per department?Solution
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.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.Final Answer:
WITH dept_salaries AS (SELECT department, salary FROM employees) SELECT department, AVG(salary) FROM dept_salaries GROUP BY department; -> Option BQuick 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]
- Omitting parentheses in CTE definition
- Not grouping by department when aggregating
- Using employees table instead of CTE in main query
