advancedrecursion MySQL MariaDB SQL Server PostgreSQL SQLite
Build A Path String While Traversing A Hierarchy
Accumulate a breadcrumb path string as a recursive CTE walks a parent-child tree.
Created Last updated 5/5 supported engines validation-green1 example2 scenarios
Docker-validated Not currently validation-green
Example 1
Generate the full path for each folder in a file system hierarchy
The anchor selects the root folder and seeds path with its name. Each recursive step finds folders whose parent_id matches a row already in paths and concatenates the parent's path, a slash, and the current folder's name. docs and images are both direct children of root, so their paths are root/docs and root/images. photos is a child of images, so its path becomes root/images/photos.
WITH
paths AS (
SELECT
id,
CAST(name ASVARCHAR(500)) ASpathFROM
folders
WHERE
parent_id ISNULLUNIONALLSELECT
f.id,
CAST(p.path + '/' + f.name ASVARCHAR(500))
FROM
folders f
JOIN paths p ON f.parent_id = p.id
)
SELECTpathFROM
paths
ORDERBYpath;
WITHRECURSIVE paths AS (SELECT id, name::textASpathFROM folders WHERE parent_id ISNULLUNIONALLSELECT f.id, p.path || '/' || f.name FROM folders f JOIN paths p ON f.parent_id = p.id) SELECTpathFROM paths ORDERBYpath;
WITHRECURSIVE
paths AS (
SELECT
id,
name ASpathFROM
folders
WHERE
parent_id ISNULLUNIONALLSELECT
f.id,
p.path || '/' || f.name
FROM
folders f
JOIN paths p ON f.parent_id = p.id
)
SELECTpathFROM
paths
ORDERBYpath;
Expected result
path
root
root/docs
root/images
root/images/photos
SQL Server omits RECURSIVE and uses + for string concatenation. PostgreSQL and SQLite use ||. MySQL and MariaDB use CONCAT with an explicit cast on the anchor column. All engines produce identical paths.
Useful when
Where this command helps.
displaying breadcrumb trails for a folder or category tree
generating full path identifiers for nodes in a hierarchical dataset
Explanation
What the command is doing.
A recursive CTE can carry computed values across levels, not just raw column data. The anchor member seeds the path with the root node's name. Each recursive step concatenates the parent's path with a separator and the current node's name. The result is a full path string for every node in the tree — useful for folder breadcrumbs, category hierarchies, and org chart paths. SQL Server omits the RECURSIVE keyword and uses + for string concatenation. SQLite and PostgreSQL use the || operator. MySQL and MariaDB use CONCAT(). To prevent silent truncation in MySQL, the anchor should cast the path column to a wide character type.