-- get all descendants of the node id:2, including the node itself SELECT * FROM test WHERE id IN ( SELECT dst.id FROM test_tree src INNER JOIN test_tree dst ON tree_distance(dst.node, src.node) >= 0 WHERE src.id = 2 );