SQL CTEs and recursive queries for hierarchical data
11 steps · about 3 h 45 min · Intermediate
- SQLite CTE (article, SQLite Tutorial, 15 min)
Start with the plain WITH clause: name a query, then use it like a table, with runnable examples on a sample database.
- SQL CTEs: WITH Clause Syntax, Examples & Practice (article, SQLabHub, 20 min)
Covers chaining several CTEs, the common mistake of repeating WITH, and quizzes to check you understood the syntax.
- SQL CTE (WITH Clause): The Ultimate Guide (article, Database Star, 20 min)
Walks through CTE examples line by line and compares syntax across Oracle, SQL Server and MySQL before you meet recursion.
- WITH clause (Recursive Common Table Expressions) (article, SQLite documentation, 25 min)
The authoritative explanation of ordinary versus recursive CTEs, including the rule that a recursive select references the CTE exactly once.
- SQL Recursive Hierarchy Query: Key Concepts (article, Dataforge Labs, 15 min)
Introduces the three parts of a recursion (anchor, recursive member, termination check) with a simple counting example.
- Recursive CTEs — MySQL for Developers (video, PlanetScale, 15 min)
A video lesson that builds a number sequence and a tree by hand, so you can see each iteration.
- Recursive CTEs for Hierarchical Data Concept (article, SQL Practice Online, 15 min)
Explains iteration by iteration why a recursive CTE beats a fixed number of self-joins when depth varies.
- Hierarchical Data and How to Query It in SQL (article, LearnSQL.com, 20 min)
Applies the pattern to employee and folder trees, including building a path column.
- Understanding SQL Server Recursive CTE By Practical Examples (article, SQL Server Tutorial, 15 min)
Shows the classic manager-to-subordinates query and the SQL Server dialect, which omits the RECURSIVE keyword.
- Learn to Use a Recursive CTE in SQL Query (article, StrataScratch, 25 min)
Gives you several real-world patterns (employee tree, product categories, file folders) with a level counter that caps recursion depth.
- WITH Queries (Common Table Expressions) (doc, PostgreSQL documentation, 40 min)
Finish with the precise evaluation algorithm, plus the SEARCH and CYCLE clauses for ordering results and stopping infinite loops.