BeginnerCoder

Lesson 23 — Recursive hierarchy with depth

Depth and path in a hierarchy

A recursive CTE can carry extra columns from level to level. Add depth (anchor 0, then depth + 1) and a path string that grows with ||, SQLite's text-join operator: path || ' > ' || name. Sorting by path then prints a readable tree.

SELECT 'Ann' || ' > ' || 'Bo'   -- 'Ann > Bo'

Why it matters: breadcrumb navigation ("Home > Books > Python") and indented org charts come straight from this pattern.

Want it another way?
Task

In the recursive step, make depth one more than the parent's and extend the parent's path with the new name. Expected: an indented tree from Ann down.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with SQL Deep Dive

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.