SQL • LESSON 31
Common Table Expressions
Learn how to organize complex SQL queries using reusable query blocks.
🔴 Advanced⏱ 3 Minutes⭐ 490 XP
🤔 THE QUESTION
How can we break a complex SQL query into smaller, easier-to-understand steps?
💡 WHAT IS IT?
A Common Table Expression, or CTE, creates a temporary named result that can be referenced by the main query.
🎯 WHAT IS IT USED FOR?
CTEs are useful for making complex queries easier to read, organizing multi-step logic, and reusing intermediate query results.
💻 EXAMPLE
WITH high_paid AS (
SELECT name,
salary
FROM employees
WHERE salary > 100000
)
SELECT *
FROM high_paid;Mission Brief
Practice creating a CTE to identify employees whose salary is above 100000.
Objectives
- ✓ WITH
- ✓ CTE
- ✓ SELECT
- ✓ Filtering
- ✓ Query organization