All articles
sql2026-04-28

CTE vs Subquery in Snowflake — Which is Faster and Why

Contents

"Use CTEs instead of subqueries — they're faster because they materialize the result" — this claim has circulated for years. In Snowflake, it's false. Understanding why — and when the choice between them actually matters — is the difference between SQL that works and SQL that works well.

What is a subquery?

A subquery is a SQL query nested inside another query. It can appear in FROM, WHERE, or even as a column in SELECT.

-- Subquery in FROM
SELECT c.country, COUNT(*) AS orders
FROM orders o
JOIN (
  SELECT customer_id, country
  FROM customers
  WHERE status = 'active'
) c ON o.customer_id = c.customer_id
GROUP BY c.country;

-- Subquery in WHERE
SELECT product_id, name
FROM products
WHERE category_id IN (
  SELECT id FROM categories WHERE parent_category = 'Electronics'
);

What is a CTE (Common Table Expression)?

A CTE is a named temporary result set defined at the start of a query using the WITH clause. You can then use it like a table — multiple times, by name.

WITH active_customers AS (
  SELECT customer_id, country
  FROM customers
  WHERE status = 'active'
)
SELECT c.country, COUNT(*) AS orders
FROM orders o
JOIN active_customers c ON o.customer_id = c.customer_id
GROUP BY c.country;

The result is identical to the subquery — but more readable. That's the key argument for CTEs.

Is CTE faster than subquery in Snowflake?

Short answer: no. Snowflake's optimizer treats a CTE as a logical alias — it "unpacks" it and inlines it into the execution plan. Result: identical query plans.

This is one of the most persistent myths in SQL — a myth that originates from PostgreSQL, where CTEs historically WERE materialized by default. In Snowflake (and most modern databases), the optimizer is smarter.

The only real advantage of CTE — readability and reusability

CTE wins on one argument: you can use it multiple times in the same query without repeating code. Using the same subquery twice without a CTE means writing that logic twice — CTEs eliminate the duplication.

When CTE can be slower — the multiple-use trap

Paradox: using a CTE twice doesn't guarantee it executes only once.

If Snowflake's optimizer decides to re-execute the CTE for each reference, you'll scan the source table multiple times. The only guarantee of single execution is a temporary table.

The only real materialization: TEMPORARY TABLE

-- Compute once, guaranteed materialization
CREATE OR REPLACE TEMPORARY TABLE tmp_order_stats AS
SELECT
  customer_id,
  COUNT(*)        AS order_count,
  SUM(revenue)    AS total_revenue,
  AVG(revenue)    AS avg_order_value,
  MAX(order_date) AS last_order_date
FROM orders
WHERE order_date >= '2024-01-01'
GROUP BY customer_id;

-- Use as many times as you want — without re-scanning orders
SELECT c.name, t.order_count, t.avg_order_value
FROM customers c
JOIN tmp_order_stats t ON c.id = t.customer_id;

TEMPORARY TABLE lives only within the session — it doesn't pollute your schema.

One case where subquery beats CTE syntactically

A subquery in a WHERE clause with IN or EXISTS can't be replaced by a CTE without changing structure:

-- Natural as a subquery
SELECT * FROM products
WHERE category_id IN (SELECT id FROM categories WHERE active = TRUE);

-- With CTE you need a JOIN (different structure)
WITH active_cats AS (SELECT id FROM categories WHERE active = TRUE)
SELECT p.* FROM products p
JOIN active_cats ac ON p.category_id = ac.id;

Both give the same result, but the subquery with IN is more intuitive for filtering.

How to find what's actually slow

Instead of guessing — use Query Profile in Snowflake: run the query → click the "Query Profile" chart icon → look for nodes taking >50% of time.

Most common real bottlenecks: missing date filter → full table scan; the same subquery computed multiple times → candidate for TEMP TABLE.

TL;DR

  • CTE = logical alias in Snowflake, NOT automatic materialization
  • Identical query plan to subquery in 99% of cases
  • Use CTEs for readability and code deduplication — not for performance
  • Multiple CTE references may result in multiple scans — check Query Profile
  • Guaranteed materialization = CREATE TEMPORARY TABLE AS SELECT
  • Subquery in WHERE IN/EXISTS — one place where subquery has a natural syntactic advantage
  • Before optimizing: Query Profile → find the actual bottleneck

Is your data scattered across several places?

I help build a warehouse and pipelines that refresh on their own. The first call is free.

Book a consultation

More articles about data analytics:

Back to blog