Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL Joins Exercises: Display the department name, manager name, and city

MySQL Joins: Exercise-9 with Solution

Write a query to display the department name, manager name, and city.

Sample table: employees


Sample table: departments


Sample table: locations


Code:

SELECT d.department_name, e.first_name, l.city 
FROM departments d 
JOIN employees e 
ON (d.manager_id = e.employee_id) 
JOIN locations l USING (location_id);

Relational Algebra Expression:

Relational Algebra Expression: Join: Display the department name, manager name, and city.

Relational Algebra Tree:

Relational Algebra Tree: Join: Display the department name, manager name, and city.

Sample Output:

department_name		first_name	city
Administration		Jennifer	Seattle
Marketing		Michael		Toronto
Purchasing		Den		Seattle
Human Resources		Susan		London
Shipping		Adam		South San Francisco
IT			Alexander	Southlake
Public Relations	Hermann		Munich
Sales			John		OX9 9ZB
Executive		Steven		Seattle
Finance			Nancy		Seattle
Accounting		Shelley		Seattle

 

MySQL Code Editor:

Structure of 'hr' database :

hr database

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

Previous:Write a query to display the department ID and name and first name of manager.
Next:Write a query to display the job title and average salary of employees.

What is the difficulty level of this exercise?