WITH RECURSIVE tree (id, parent, internal_name, level, path) AS ( SELECT root node UNION SELECT recursion JOIN some_name on some_name.field = root.other_field ) SELECT * from tree order by path;