A CTE that references itself for hierarchies or graph traversals.
A self-referencing CTE that iteratively generates rows, used for traversing hierarchies (org charts, file systems) and graphs. The recursive part unions with the base case until convergence. Always include a termination condition.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | 8.0 | RECURSIVE keyword required |
| PostgreSQL | ✓ Supported | 8.4 | Standard RECURSIVE CTE |
| SQL Server | ✓ Supported | 2005 | Standard CTE recursion |
| Oracle | ✓ Supported | 11g R2 | Pre-11gR2 use CONNECT BY |
| SQLite | ✓ Supported | 3.8.3 | Supports RECURSIVE CTEs |
Great for org charts, file trees, and any hierarchical data.
RECURSIVE keyword required
Standard RECURSIVE CTE
Standard CTE recursion
Pre-11gR2 use CONNECT BY
Supports RECURSIVE CTEs