SELECT n.name, COUNT(*)-1 AS level FROM tree AS n, tree AS p WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;