Common Table Expressions (CTEs) provide a way to write auxiliary statements for use in a larger query.
Defines named temporary result sets for use within a single query, improving readability by extracting complex logic into readable named blocks. Recursive CTEs can traverse hierarchical data (org charts, trees). Non-recursive CTEs are optimized away if referenced once.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | 8.0 | Added in MySQL 8.0 |
| PostgreSQL | ✓ Supported | 8.4 | Well supported since PostgreSQL 8.4 |
| SQL Server | ✓ Supported | 2005 | Available since SQL Server 2005 |
| Oracle | ✓ Supported | 9i Release 2 | Available since Oracle 9i R2 |
| SQLite | ✓ Supported | 3.8.3 | Added in SQLite 3.8.3 |
Common Table Expressions (CTEs) are named temporary result sets that exist within the scope of a single statement.
Added in MySQL 8.0
Well supported since PostgreSQL 8.4
Available since SQL Server 2005
Available since Oracle 9i R2
Added in SQLite 3.8.3