A hierarchical query using a recursive CTE walks a table that points at itself, one level at a time. This post builds the levels of a small staff table with Lvl + 1, sorts the tree so each person sits under their boss, walks upward from one person and stops a loop in the data. Every result comes from SQL Server 2025.
CTE Referenced Twice: Why the Query Runs It Twice
Writing a CTE name two times can mean doing the work two times. Two small demos show when a temp table helps and when it only adds cost.
Building an Org Chart Query With hierarchyid
Build an org chart with hierarchyid, query descendants and levels, move a team safely, and compare it with recursive queries.
Translating Oracle CONNECT BY Queries to Recursive CTEs
Translate CONNECT BY into recursive CTEs with root selection, depth, paths, sibling ordering, cycle checks, and a recursion limit.
Convert Subquery to CTE: SQL in Sixty Seconds #001
You can convert subquery to CTE syntax by naming the inner query and using that name in the outer query. The first SQL in Sixty Seconds episode shows the change. My tests on SQL Server 2025 show the result stays the same, and that a CTE used twice reads the table twice.







