Search code examples
sqloracle-databaseinner-joinself-join

Oracle SQL Query - Replace ID by Name


I have a table group containing groups with their parent group:

groupid   name   parentid
-------------------------
   1      test       1 
   2      second     1
   3      3rd        1
   4      next       2

How can I query this table to receive a result like this (name instead id)

groupid   name     parent
---------------------------
   1      test     test
   2      second   test
   3      3rd      test
   4      next     second

Solution

  • You can use below Query:

    select A.groupid, A.name, B.name as parentid
    from group A
    left join group B on A.groupid = B.parentid