Common Table Expressions (CTEs)
A Common Table Expression (CTE) is a temporary result set created with the WITH clause.
CTEs make your SQL easier to read, organize, and reuse.
In this lesson, we'll look at non-recursive CTEs.
This means the CTE does not call itself — it is used once for simple logic or filtering.
Syntax
The basic syntax for a CTE is as follows:
WITH cte_name AS ( SELECT ... FROM ... WHERE ... ) SELECT * FROM cte_name;
- The
WITHclause defines the CTE block - The main query can then reference it as if it were a regular table
Example: Users who selected 2 or more courses
The following query returns users who selected two or more courses.
WITH multi_course_users AS ( SELECT user_id, COUNT(*) AS course_count FROM user_courses GROUP BY user_id HAVING COUNT(*) >= 2 ) SELECT u.name, m.course_count FROM multi_course_users m JOIN users u ON u.user_id = m.user_id ORDER BY u.name;
Output:
| name | course_count |
|---|---|
| Aisha | 2 |
| Sofia | 2 |
This query:
- Creates a CTE (
multi_course_users) to find users with at least two courses. - Joins it with the
userstable to display names and course counts.
Benefits of Using CTEs
- Readability: Breaks a large query into smaller, manageable parts
- Modularity: Allows you to reuse and test query components
- Maintainability: Makes it easier to update logic when separated
Multiple CTEs
You can define multiple CTEs in a single query by separating them with commas:
WITH active_users AS ( SELECT DISTINCT user_id FROM user_courses ), recent_users AS ( SELECT * FROM users WHERE name LIKE 'S%' ) SELECT * FROM active_users a JOIN recent_users r ON a.user_id = r.user_id;
This query joins the two CTEs on the user_id column.
What is the primary purpose of using Common Table Expressions (CTEs) in SQL?
To permanently store data in the database.
To execute multiple queries simultaneously.
To simplify complex queries by breaking them into smaller parts.
To increase the speed of SQL query execution.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Tables
Execution Result