How can we build a multi-step SQL transformation using CTEs?
Chaining sequential CTEs creates a multi-stage data pipeline (filtering → aggregation → final thresholding).
Production ELT pipelines, dbt model transformations, warehouse layer staging.
WITH active_employees AS (
SELECT *
FROM employees
WHERE status = 'Active'
),
department_stats AS (
SELECT
department_id,
COUNT(*) AS employee_count,
AVG(salary) AS average_salary
FROM active_employees
GROUP BY department_id
)
SELECT *
FROM department_stats
WHERE employee_count > 5
AND average_salary > 80000;Practice typing production-grade SQL code for CTE Pipeline.