Editor

Employees and their managers

Medium · Company database · Self join, LEFT JOIN

An employee's manager_id is the id of another employee in the same table. The chief executive has no manager.

Return each employee's name and their manager's name, in that order, sorted by employee name A to Z. An employee without a manager appears with NULL as the manager.

Expected result

employeemanager
Ana ReyesJoseph Mwangi
Arjun PatelVikram Nair
Carlos MendozaJoseph Mwangi
Daniel OkoyeMeera Krishnan
Ethan BrooksLaura Chen
Eva NovakKenji Watanabe
Fatima RahmanDaniel Okoye
Grace LiuRahul Bansal
Hannah SchmidtMeera Krishnan
Ines MoreauHannah Schmidt
Joseph MwangiMeera Krishnan
Kenji WatanabeMeera Krishnan
Laura ChenDaniel Okoye
Lucas FerreiraSophie Laurent
Maria GonzalezVikram Nair
Meera KrishnanNULL
Mohammed HassanRahul Bansal
Nadia PetrovaThandiwe Nkosi
Oliver HughesSophie Laurent
Priyanka IyerSophie Laurent
Rahul BansalMeera Krishnan
Ravi ShankarKenji Watanabe
Samuel AdeyemiThandiwe Nkosi
Sophie LaurentMeera Krishnan
Thandiwe NkosiSophie Laurent
Vikram NairDaniel Okoye
Zara AhmedLaura Chen
The company tables

assignments: 30 rows, first 5 shown

idemployee_idproject_idhours
181320
291410
3111380
4122290
5102150

departments: 8 rows, first 5 shown

idnamelocation
1EngineeringBengaluru
2SalesLondon
3MarketingLondon
4FinanceSingapore
5Human ResourcesBengaluru

employees: 27 rows, first 5 shown

idnamedepartment_idmanager_idjob_titlesalaryhire_date
1Meera KrishnanNULLNULLChief Executive Officer2500002015-03-01
2Daniel Okoye11Chief Technology Officer2500002015-03-01
3Sophie Laurent21Head of Sales1500002017-01-09
4Rahul Bansal41Finance Director1660002016-11-21
5Hannah Schmidt51Human Resources Manager1100002018-04-02

projects: 10 rows, first 5 shown

idnamedepartment_idbudgetstart_dateend_date
1Payments Platform14500002025-01-062025-09-30
2Mobile App Redesign13000002025-03-03NULL
3Data Warehouse13800002025-06-022026-03-31
4Spring Campaign3900002025-02-102025-05-30
5Partner Portal21500002025-04-142025-12-19
-- Write your SQL here