hierarchal database table 1

hierarchal database table

SELECT e.id, e.first_name, e.role, e.manager_id, m.first_name
FROM employee e
LEFT JOIN employee m ON e.manager_id = m.id;

Here is what the above code is Doing:
1. We’re selecting the id, first_name, role, and manager_id columns from the employee table.
2. We’re also selecting the first_name column from the employee table, but we’re aliasing it as m.first_name.
3. We’re joining the employee table to itself using a LEFT JOIN.
4. We’re joining the employee table to itself using the manager_id column from the employee table and the id column from the employee table.
5. We’re aliasing the employee table as e and the employee table aliased as m.

Similar Posts