You are on page 1of 9

Lab # 09

Database management system


Fall 2021

Instructor Ma’am Hafsa

Student Name Amna Nadeem

CMSID 404366

Department BS CS

Semester 3rd
Lesson Set Inner, Left, Right, cross and
Multiple Join
9
Purpose 1. To get a basic awareness of Joins
2. To understand the usage of inner and left join
3. How and where we can use cross and multiple join
4. To practice and use these joins in workbench

Procedure 1. Students should read the Pre-lab Reading assignment before coming to the
lab.
2. Students should complete the Pre-lab Writing assignment before coming to
the lab.
3. Students should complete Labs 9.1 through 9.2 in sequence in the lab. Your
instructor will give further instructions on grading and completing the lab.
4. Students should complete the set of lab tasks before the next lab and get
them checked by their lab instructor.

Contents Pre-requisites Completion Page


Time Number

Pre-lab Reading Assignment - 20 min 3

Pre-lab Writing Assignment Pre-lab Reading 10 min 5

Lab 9

Lab 9.1 Pre-lab reading 30 min 5


Recalling the SQL syntax

Lab 9.2 Awareness of - 6


Lab Tasks Syntax Rules

2|Page
PRE-LAB READING ASSIGNMENT

INNER JOIN

INNER JOIN in SQL is the most common and important type of join which allows users
to access matching data from two or more database tables.
When the join condition is met between the tables, then it returns all the common rows
from them.
The Venn diagram of INNER JOIN is shown in the following picture. The shaded region
of the Venn diagram shows the intersection values of two tables:

Syntax of INNER JOIN in SQL


SELECT * FROM Table_Name1
INNER JOIN Table_Name2
ON Table_Name1.Column_Name = Table_Name2.Column_Name;

LEFT JOIN

The Left Join in MySQL is used to query records from multiple tables. This clause is
similar to the Inner Join clause that can be used with a SELECT statement immediately
after the FROM keyword. When we use the Left Join clause, it will return all the records
from the first (left-side) table, even no matching records found from the second (right
side) table. If it will not find any matches record from the right side table, then returns
null.
In other words, the Left Join clause returns all the rows from the left table and matched
records from the right table or returns Null if no matching record found. This Join can
also be called a Left Outer Join clause. So, Outer is the optional keyword to use with
Left Join.
We can understand it with the following visual representation where Left Joins returns all
records from the left-hand table and only the matching records from the right side table:

3|Page
MySQL LEFT JOIN Syntax
SELECT * FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

RIGHT JOIN

The Right Join is used to joins two or more tables and returns all rows from the right-
hand table, and only those results from the other table that fulfilled the join condition. If it
finds unmatched records from the left side table, it returns Null value. It is similar to the
Left Join, except it gives the reverse result of the join tables. It is also known as Right
Outer Join. So, Outer is the optional clause used with the Right Join.
We can understand it with the following visual representation where Right Outer Join
returns all records from the left-hand table and only the matching records from the other
table:

RIGHT JOIN Syntax


SELECT * FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id
ORDER BY customer_id;

CROSS JOIN

MySQL CROSS JOIN is used to combine all possibilities of the two or more tables and
returns the result that contains every row from all contributing tables. The CROSS JOIN
is also known as CARTESIAN JOIN, which provides the Cartesian product of all
associated tables. The Cartesian product can be explained as all rows present in the first
table multiplied by all rows present in the second table. It is similar to the Inner Join,
where the join condition is not available with this clause.
We can understand it with the following visual representation where CROSS JOIN
returns all the records from table1 and table2, and each row is the combination of rows of
both tables.

4|Page
MySQL CROSS JOIN Syntax
SELECT * FROM customers
CROSS JOIN contacts;

5|Page
Pre-lab writing assignment

Fill in the blanks 1. When the join condition is met between the tables, then it returns all
the common rows from them is called Inner Join,

2. The Left Join clause returns all the rows from the left table and
matched records from the right table or returns Null if no matching
record found.

3. The Right Join clause returns all the rows from the right table and
matched records from the left table or returns Null if no matching
record found

4. Cross Join is also called Cartesian Join.

6|Page
Lab 9.2 Lab Tasks

1. Use the Lab 7 database and write query to show all the data exist in student table and matching
data in city table.

2. Use the Lab 7 database and write query to show all the data exist in city table and matching data
in student table.

*same question as above*

3. Create a new table with the named courses and add two columns, course_id and course_name
and make the course_id column as primary key and auto_increment.

7|Page
4. Modify the student table, add new column with named course with datatype INT and also make
this column as foreign key

5. Write query for multiple join to join all the student, city, and courses table

8|Page
9|Page

You might also like