Skip to main content
SQL • LESSON 31

Common Table Expressions

How can we break complex nested queries into clean, readable, reusable modules?

Advanced3 Minutes490 XP
🤔 THE QUESTION

How can we break complex nested queries into clean, readable, reusable modules?

💡 WHAT IS IT?

A Common Table Expression (CTE) defines a temporary named result set using the WITH clause.

🎯 WHAT IS IT USED FOR?

Readable multi-step transformations, ELT pipelines in dbt/BigQuery/Snowflake, intermediate metrics.

💻 EXAMPLE
WITH high_paid AS (
  SELECT name,
  salary
  FROM employees
  WHERE salary > 100000
)
SELECT *
FROM high_paid;

🎯 Mission Objectives

Practice typing production-grade SQL code for Common Table Expressions.

  • WITH clause
  • CTE syntax
  • Query modularity
  • Clean SQL