Skip to content
Advertisement

How to find the Employee names and their supervisor names if the table doesn’t have common numeric column like Employee_id or employee number in Mysql

If the table have only two columns with employee name and their supervisor column and if it doesn’t have any other numeric or number column with employee_number or employee_id, then how the results can be produced. I’m not getting logic to show the results.

Code for creating table in Mysql:

It’s output is:

The result should be in the lower to higher level of hierarchy like:

Advertisement

Answer

For this particular set of data, it can LEFT JOIN the table itself to get the expected results

If hierarchy depth is unknown, which means the number of columns is unknown, it’s more complicated. It is still possible by using recursive CTE to find the depth and generate dynamic SQL.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement