Medium JOINs Premium

SQL interview question: Employee and Manager Self Join

Employee Reporting Structure

HR needs a report showing each employee alongside their direct manager's information for org chart visualization.

Join the employees table to itself to show each employee with their manager's name and title.

Return employee_name (full name), employee_title, manager_name (full name), and manager_title. Exclude employees with no manager. Order by the employee's id and limit to 10.

Skills: self-join for hierarchical relationships

Tables

employees 37 rows
  • id integer PK
  • first_name text
  • last_name text
  • email text
  • department_id integer → departments
  • salary integer
  • hire_date text
  • manager_id integer → employees
  • title text
idfirst_namelast_nameemaildepartment_idsalaryhire_datemanager_idtitle
1AliceChen[email protected]12500002015-01-15NULLCEO
2RobertMartinez[email protected]21800002016-03-20NULLCEO
3SarahJohnson[email protected]11500002017-06-101VP Engineering

Keep going