Walk a hierarchy.
Compute every descendant of category 1.
WITH RECURSIVE tree AS (
SELECT id, parent_id FROM categories WHERE id = 1
UNION ALL
SELECT c.id, c.parent_id FROM categories c JOIN tree t ON c.parent_id = t.id
)
SELECT * FROM tree;
Sign in to save your code and track progress across devices.