DBMS Lab 6 Tasks
DBMS Lab 6 Tasks
LAB 06
SQL Joins
Students will learn how to use different types of SQL joins by executing queries on a sample database.
Instructions:
1. Create the provided schema using the DDL script.
2. Insert sample data using the INSERT statements.
3. Write and execute queries for the following tasks:
o Use INNER JOIN to fetch employees and their department names.
o Use LEFT JOIN to display all employees and their department names, ensuring employees
without a department are also shown.
o Use RIGHT JOIN to display all departments and their employees, ensuring departments
without employees are also shown.
o Use FULL OUTER JOIN to display all employees and all departments, ensuring unmatched
records appear with NULL.
o Use NATURAL JOIN to retrieve project details along with the employees assigned to them.
-- Department Table
CREATE TABLE Department (
department_id INT PRIMARY KEY,
department_name VARCHAR(50),
min_salary DECIMAL(10,2)
);
--Employee Table
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
department_id INT, --Foreign key referencing department
salary DECIMAL(10,2),
Foreign key(department_id) references Department(department_id)
);
-- Project Table
CREATE TABLE Project (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
emp_id INT -- Foreign key referencing Employee
Foreign key(emp_id) references Employee (emp_id)
);
Tasks:
1. Retrieve employees and their respective department names.
2. Retrieve all employees, including those who are not assigned to any department.
3. Retrieve all departments, including those without employees.
4. Retrieve all employees and departments information.
5. Retrieve project details along with employees working on them.