Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Hamza Nazir DB Lab 11

DATA BASE
LAB REPORT: 11
Submitted By: Hamza Nazir (2872)
Submitted To: Sir Yasir
DATE:——-

Bscs-40B (5th Semester) Evening

Lab Report: Exploring SQL Joins in the HR Schema


Job History table

Hamza Nazir DB Lab 11 1


employees table

location table

department table

Hamza Nazir DB Lab 11 2


Objective:
The objective of this lab is to explore the usage of inner join and outer join in SQL queries applied to the HR schema. These queries
aim to extract meaningful insights from the HR database regarding employees, departments, job titles, and their relationships.
Task 1: Inner Join Examples

1. Employees and their job histories:

SQL Query:

SELECT e.employee_id, e.first_name, jh.start_date, jh.end_date


FROM employees e
INNER JOIN job_history jh
ON e.employee_id = jh.employee_id;

Output:
This query retrieves the job history for each employee, including their start date and end date for each position held.

2. Departments and their locations:

SQL Query:

Hamza Nazir DB Lab 11 3


SELECT d.department_id, d.department_name, l.location_id, l.city
FROM departments d
INNER JOIN locations l
ON d.location_id = l.location_id;

Output:
It provides the location details for each department, including the location ID and city where each department is situated.

3. Employees and their job titles and departments:

SQL Query:

SELECT e.employee_id, e.first_name, j.job_title, d.department_name


FROM employees e
INNER JOIN jobs j
ON e.job_id = j.job_id
INNER JOIN departments d
ON e.department_id = d.department_id;

Output:
This query combines employee data with their respective job titles and departments, providing a comprehensive view of
each employee's role within the organization.

Hamza Nazir DB Lab 11 4


Task 2: Outer Join Examples

1. All employees and their job histories (including employees without job history):

SQL Query:

SELECT e.employee_id, e.first_name, jh.start_date, jh.end_date


FROM employees e
LEFT OUTER JOIN job_history jh
ON e.employee_id = jh.employee_id;

Output:
It retrieves the job history for each employee, including those who do not have any job history records.

2. All departments and their employees (including departments without employees):

SQL Query:

Hamza Nazir DB Lab 11 5


SELECT d.department_id, d.department_name, e.first_name
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id;

Output:
This query provides a list of all departments, including those without any assigned employees, along with the names of
employees assigned to each department.

3. Employees and their managers and departments (including employees without managers or departments):

SQL Query:

SELECT e.employee_id, e.first_name, m.first_name AS manager_name, d.department_name


FROM employees e
LEFT OUTER JOIN employees m
ON e.manager_id = m.employee_id
LEFT OUTER JOIN departments d
ON e.department_id = d.department_id;

Output:
It lists all employees along with their managers and departments, even including employees without assigned managers or
departments.

Hamza Nazir DB Lab 11 6


4. Locations and their departments (including locations without departments):

SQL Query:

SELECT l.location_id, l.city, d.department_name


FROM locations l
RIGHT OUTER JOIN departments d
ON l.location_id = d.location_id;

Output:
This query provides a list of all locations, including those without any assigned departments, along with the names of
departments assigned to each location.

Hamza Nazir DB Lab 11 7


5. Departments and their employees and locations (including departments without employees or locations):

SQL Query:

SELECT d.department_id, d.department_name, e.first_name, l.city


FROM departments d
FULL OUTER JOIN employees e
ON d.department_id = e.department_id
FULL OUTER JOIN locations l
ON d.location_id = l.location_id;

Output:
It lists all departments, including those without any assigned employees or locations, along with the names of employees
and city names where each department is located.

Hamza Nazir DB Lab 11 8


Conclusion:
Through these SQL queries utilizing inner join and outer join, we've gained insights into employee details, job histories, department
locations, and organizational structures within the HR schema. Inner join allows us to retrieve data where relationships exist, while
outer join ensures inclusion of all relevant data, even in cases of missing relationships.

Hamza Nazir DB Lab 11 9

You might also like