DBMS Manual-Exp - 8
DBMS Manual-Exp - 8
Experiment -8
JOINS : join is used to combine columns from one (self-join) or more tables based on the
values of the common columns between the tables. A JOIN is a means for combining fields
from two tables by using values common to each. There are four types of joins :-
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
Sample tables: Suppose we have two tables called basket_ a and basket_ b that stores fruits:
CREATE TABLE basket a (id INT PRIMARY KEY, fruit VARCHAR (100) NOT NULL);
CREATE TABLE basket_b (id INT PRIMARY KEY, fruit VARCHAR (100) NOT NULL);
INNER JOIN : This type of join returns those records which have matching values in both
tables.
DR SANGEETA SONI 38
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
SELECT a.id id a, a.fruit fruit a, b.id id b, b.fruit fruit b FROM basket a a INNER
JOIN basket_b b ON a.fruit = b.fruit;
Output :
id_a _a I _b _b
1 Apple 2 Apple
2 Orange 1 Orange
LEFT JOIN: Returns all rows from the left table, even if there are no matches in the right
table.
SELECT a.id id a, a.fruit fruit a, b.id id b, b.fruit fruit b FROM basket a a LEFT JOIN
basket b b ON a.fruit = b.fruit;
Output :
id_a _a id_b _b
Apple 2 Apple
2 Orange 1 Orange
RIGHT JOIN : Returns all rows from the right table, even if there are no matches in the left
table.
DR SANGEETA SONI 39
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
SELECT a.id id a, a.fruit fruit a, b.id id b, b.fruit fruit b FROM basket a a RIGHT
JOIN basket_b b ON a.fruit = b.fruit;
Output :
id_a _a id_b _b
2 orange 1 Orange
1 apple 2 Apple
FULL JOIN: Returns rows when there is a match in one of the tables.
SELECT a.id id a, a.fruit fruit a, b.id id b, b.fruit fruit b FROM basket a a FULL
JOIN basket_b b ON a.fruit = b.fruit WHERE a.id IS NULL OR b.id IS NULL;
Output :
id a _a id_b
DR SANGEETA SONI 40
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
VIVA OUESTIONS-
Inner Join: This method returns datasets that have the same values in both tables.
Full Join: Also known as a full outer join. It combines all rows from the left table and
the right table to create the result set.
Right Join: Also known as the right outer join. All records from the right table will be
returned, as well as any records that match from the left table.
Left Join: Also known as left outer join. All records from the left table will be returned,
as well as any records that match from the right table.
The LEFT JOIN includes all records from the left side and matched rows from the right
table, whereas the RIGHT JOIN returns all rows from the right side and matched rows
from the left table.
SQL JOINS are key methods to integrate multiple tables to make easy to read and they
provide an efficient and accessible way to access and combine information in the
database.
A CROSS JOIN returns a paired combination of each row of the first table with each
row of the second table. This join type is also known as cartesian join.
Yes, this is normally done through a so-called self-join. A self-join is a type of JOIN
used to compare rows within the same table.
A FULL JOIN returns all records from both tables. When the ON condition is not
satisfied, it returns a NULL value. By contrast, a CROSS JOIN returns all possible
combinations of all rows of both tables, resulting in a cartesian product between the two
tables.
DR SANGEETA SONI 41