PostgreSQL JOINS: Make a join with three tables to find the name, jobs, department name and ID of all the employees working in London
3. Write a SQL query to make a join with three tables employees, departments and locations to find the name, including first_name and last_name, jobs, department name and ID, of the employees working in London.
Sample Solution:
Code:
SELECT e.first_name, e.last_name, e.job_id, e.department_id, d.department_name
FROM employees e
JOIN departments d
ON (e.department_id = d.department_id)
JOIN locations l ON
(d.location_id = l.location_id)
WHERE l.city = 'London';
Sample table: employees
Sample table: locations
Sample table: departments
Output:
pg_exercises-# FROM employees e pg_exercises-# JOIN departments d pg_exercises-# ON (e.department_id = d.department_id) pg_exercises-# JOIN locations l ON pg_exercises-# (d.location_id = l.location_id) pg_exercises-# WHERE l.city = 'London'; first_name | last_name | job_id | department_id | department_name ------------+-----------+--------+---------------+----------------- Susan | Mavris | HR_REP | 40 | Human Resources (1 row)
Relational Algebra Expression:
Relational Algebra Tree:
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a query to make a join with employees and departments table to find the name of the employee, including first_name and last name, department ID and name of departments.
Next: Write a query to make a join with two tables employees and itself to find the employee id, last_name as Employee along with their manager_id and last name as Manager.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework