0% found this document useful (0 votes)
6 views

Lab Report 5 Final

dbms lab report in mysql focusing on joining

Uploaded by

it22016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab Report 5 Final

dbms lab report in mysql focusing on joining

Uploaded by

it22016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Mawlana Bhashani Science and Technology University

Santosh, Tangail-1902

Department of Information and Communication Technology

Lab Report 05
Course Title: Database Management System Lab
Course Code: ICT-2108
Lab Report on: Different Types of Joining Operations in MySQL

Submitted By Submitted To

Name: Md. Faim Montasir Dr. Md. Abir Hossain


ID: IT-22016 Associate Professor,
2nd Year, 1st Semester Dept. of ICT, MBSTU
Session: 2021-2022
Dept. of ICT, MBSTU

Date of Performance: 25-03-2024 Date of Submission: 01-04-2024

Experiment No: 05
Experiment Name: Different Types of Joining Operations in MySQL

Objectives: We will learn how to join two tables using different types of joining. We
will also learn how to use joining operations with specific and required conditions to
find desired output.
Required Instruments:
1. MySQL 8.3 Command line Client.

For the purpose of joining, a classroom table is created and data has been inserted in
that table.

Inner join:
SQL expression: select dept_name,
budget, room_number,
capacity from department inner
join classroom;

Here, this query selects the department name, budget, room number, and classroom
capacity by joining the department and classroom tables based on the shared
“room_number” field.

SQL expression: select dept_name, budget, room_number, capacity from department


inner join classroom on department.building= classroom.building;
Here, this query selects the department name, budget, classroom’s room number, and
classroom’s capacity by joining the two tables (department and classroom) based on
shared “building” field. This will return data only where the department and classroom
are located in the same building.

SQL expression: select name, course_id, salary, year from instructor inner join teaches on
instructor.ID = teaches.ID;

This query selects the instructor's name, course ID, salary, and the year they taught the
course, joining the instructor and teaches tables using the common ID field.

Left join:
SQL expression: select instructor.ID, instructor.name, department.building from
instructor left join department on instructor.dept_name= department.dept_name;
This query performs a left join between the instructor and department tables based on
the “dept_name” field. The left join ensures that all records from the instructor table
are included, even if there’s no corresponding entry in the department table.

SQL expression: select classroom.room_number, department.dept_name from classroom


left join department on classroom.building= department.building;
This query performs a left join between the classroom and department tables based on
the “building” field. The left join ensures that all records from the classroom table are
included, even if there’s no corresponding entry in the department table

SQL expression: select department.dept_name, department.budget,


classroom.room_number, classroom.capacity from classroom left join department on
classroom.building = department.building;
This query uses a LEFT JOIN to combine rows from the classroom and department
tables based on the common building column. The LEFT JOIN ensures that all rows
from the classroom table are included, even if there is no matching building in the
department table, with NULL values being returned for the dept_name and budget in
such cases.

Right join:

SQL expression: select teaches.course_id, teaches.sec_id, instructor.name from teaches


right join instructor on teaches.ID=instructor.ID;
This query uses a Right Join, which ensures that all rows from the instructor table are
returned, even if there is no matching ID in the teaches table. If no match is found, the
course_id and sec_id from the teaches table will be NULL.

SQL expression: select department.building, department.budget, instructor.ID,


instructor.name from department right join instructor on
department.dept_name=instructor.dept_name;

departmen
t
The Right Join ensures that all rows from the instructor table are returned, even if
there is no matching dept_name in the department table. If no match is found, the
building and budget from the department table will be NULL

SQL expression: select department.dept_name, department.budget,


classroom.room_number, classroom.capacity from classroom right join department on
classroom.building = department.building;

The RIGHT JOIN ensures that all rows from the department table are returned, even if
there is no matching building in the classroom table. If no match is found in the
classroom table, the room_number and capacity will return NULL.
Full join:
SQL expression: select *from classroom left join department on classroom.building =
department.building union : select *from classroom right join department on
classroom.building = department.building;
The above query is a full join that combines the results of two different joins—LEFT
JOIN and RIGHT JOIN—using the UNION operator. This allows you to retrieve all
unique rows from both joins meaning it will have all the data from both classroom and
department.

SQL expression: select * from department left join course on department.dept_name =


course.dept_name union select * from department right join course on department.dept_name
= course.dept_name;

The above query is a full join that combines the results of two different joins—LEFT
JOIN and RIGHT JOIN—using the UNION operator. This allows you to retrieve all
unique rows from both joins meaning it will have all the data from both course and
department.

SQL expression: select * from instructor left join teaches on instructor.ID =


teaches.ID union select * from instructor right join teaches on instructor.ID =
teaches.ID;
The above query is a full join that combines the results of two different joins—LEFT
JOIN and RIGHT JOIN—using the UNION operator. This allows you to retrieve all
unique rows from both joins meaning it will have all the data from both teaches and
instructor.
Natural join:
SQL expression: select dept_name, budget, room_number,capacity from department natural
join classroom;
This SQL query is a natural join which will combine the department and classroom
tables based on the common columns that has the same in both department and
classroom.

SQL expression: select name, ID, dept_name, semester, year from instructor natural join
teaches;

This SQL query is a natural join which will combine the department and classroom
tables based on the common columns that has the same in both instructor and teaches.

SQL expression: select course_id, title, room_number, time_slot_id from course natural
join section;
This SQL query is a natural join which will combine the department and classroom
tables based on the common columns that has the same in both course and section.

Discussion:
By the above query we can use various types of joining like inner join, left join, right join,
natural join, full join to find desired set of data from databases.

You might also like