Helix Systems' HR team is expanding the employee directory to include management context.
Write a query to return each employee's name alongside their manager's name and the manager's job title.
Assumptions:
- The
employeestable contains every active and former employee at Helix Systems. - Both the employee role and the manager role come from the same
employeestable — the manager-side alias also exposes thetitlecolumn. - Employees with no manager (
manager_idisNULL) are excluded.
Output:
- One row per employee with a manager, with columns
employee_name,manager_name, andmanager_title.
Schema · hr 4 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
e.name AS employee_name,
m.name AS manager_name,
m.title AS manager_title
FROM
employees e
JOIN employees m ON e.manager_id = m.id The shape
Each alias is a fully independent reference to employees, so the manager role can expose both name and title even though they come from the same physical table as the employee's row.
Clause by clause
SELECT e.name AS employee_name, m.name AS manager_name, m.title AS manager_titlepulls three columns from two aliased instances.e.namecomes from the employee role;m.nameandm.titleboth come from the manager role and resolve against the manager's row, not the employee's.FROM employees ereads the table in the employee role.JOIN employees m ON e.manager_id = m.idreads the same table again in the manager role. Every column onm(name,title,manager_id,department_id, anything else) is available on this side of the join and resolves to the manager's row.
The trap
A self-join is not a copy of the table with a few columns exposed. Both aliases see the full row. The reader who thinks m is somehow restricted to id and name because that's what the join condition mentions will be surprised by m.title working without any extra setup. Once an alias is declared, every column on that table is reachable through it.
You practiced selecting two different columns from the same physical table by reaching into both aliased instances. The recurring rule: each alias is a fully independent reference — e.name and m.title come from the same table but two different rows.