All Subcategories
Hard · Recursive Queries
A recursive CTE walks a tree. WITH RECURSIVE t AS (anchor UNION ALL recursive_part): the anchor query gives the starting rows, and the recursive part joins the table to the rows found in the previous step, until no new rows appear.
categories is a tree: parent_id points to the parent category (NULL for a top-level category). Find every category below Electronics, at any depth (not Electronics itself).
Return name and depth (1 for a direct child of Electronics, 2 for a grandchild, and so on), sorted by depth, then name.