w3resource
SQL exercises

SQL JOINS on HR Database: Display the first name of all employees including the first name of their manager

SQL JOINS on HR Database: Exercise-8 with Solution

8. Write a query in SQL to display the first name of all employees including the first name of their manager.

Sample table: employees


Sample Solution:

SELECT E.first_name AS "Employee Name", 
   M.first_name AS "Manager"
     FROM employees E 
       JOIN employees M
         ON E.manager_id = M.employee_id;

Sample Output:

Employee Name	Manager
Neena		Steven
Lex		Steven
Alexander	Lex
Bruce		Alexander
David		Alexander
Valli		Alexander
Diana		Alexander
Nancy		Neena
Daniel		Nancy
John		Nancy
Ismael		Nancy
Jose Manuel	Nancy
Luis		Nancy
Den		Steven
Alexander	Den
Shelli		Den
Sigal		Den
Guy		Den
Karen		Den
Matthew		Steven
Adam		Steven
Payam		Steven
Shanta		Steven
Kevin		Steven
Julia		Matthew
Irene		Matthew
James		Matthew
Steven		Matthew
Laura		Adam
Mozhe		Adam
James		Adam
TJ		Adam
Jason		Payam
Michael		Payam
Ki		Payam
Hazel		Payam
Renske		Shanta
Stephen		Shanta
John		Shanta
Joshua		Shanta
Trenna		Kevin
Curtis		Kevin
Randall		Kevin
Peter		Kevin
John		Steven
Karen		Steven
Alberto		Steven
Gerald		Steven
Eleni		Steven
Peter		John
David		John
Peter		John
Christopher	John
Nanette		John
Oliver		John
Janette		Karen
Patrick		Karen
Allan		Karen
Lindsey		Karen
Louise		Karen
Sarath		Karen
Clara		Alberto
Danielle	Alberto
Mattea		Alberto
David		Alberto
Sundar		Alberto
Amit		Alberto
Lisa		Gerald
Harrison	Gerald
Tayler		Gerald
William		Gerald
Elizabeth	Gerald
Sundita		Gerald
Ellen		Eleni
Alyssa		Eleni
Jonathon	Eleni
Jack		Eleni
Kimberely	Eleni
Charles		Eleni
Winston		Matthew
Jean		Matthew
Martha		Matthew
Girard		Matthew
Nandita		Adam
Alexis		Adam
Julia		Adam
Anthony		Adam
Kelly		Payam
Jennifer	Payam
Timothy		Payam
Randall		Payam
Sarah		Shanta
Britney		Shanta
Samuel		Shanta
Vance		Shanta
Alana		Kevin
Kevin		Kevin
Donald		Kevin
Douglas		Kevin
Jennifer	Neena
Michael		Steven
Pat		Michael
Susan		Neena
Hermann		Neena
Shelley		Neena
William		Shelley

Practice Online


HR database model

Query Visualization:

Duration:

Query visualization of Display the first name of all employees including the first name of their manager - Duration

Rows:

Query visualization of Display the first name of all employees including the first name of their manager - Rows

Cost:

Query visualization of Display the first name of all employees including the first name of their manager - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query in SQL to display the first and last name and salary for those employees who earn less than the employee earn whose number is 182.
Next: Write a query in SQL to display the department name, city, and state province for each department.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming