Skip to main content
SQL • LESSON 104

Multiple CTEs

How can we prepare two datasets before joining them?

Advanced3 Minutes1170 XP
🤔 THE QUESTION

How can we prepare two datasets before joining them?

💡 WHAT IS IT?

Comma-separated CTE definitions allow multiple named temporary datasets within a single query.

🎯 WHAT IS IT USED FOR?

Preparing independent subsets before joining, intermediate metric staging in dbt pipelines.

💻 EXAMPLE
WITH active_employees AS (
    SELECT *
    FROM employees
    WHERE status = 'Active'
),
high_earners AS (
    SELECT *
    FROM employees
    WHERE salary > 100000
)
SELECT a.name, a.salary
FROM active_employees a
JOIN high_earners h
ON a.employee_id = h.employee_id;

🎯 Mission Objectives

Practice typing production-grade SQL code for Multiple CTEs.

  • Multiple CTEs
  • Comma-separated WITH
  • Two-step staging
  • Modular data merging