Skip to main content
SQL โ€ข LESSON 104

Multiple CTEs

How can we prepare two datasets before joining them?

Advancedโฑ 3 Minutesโญ 1170 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