0% found this document useful (0 votes)
19 views4 pages

DBMS Manual-Exp - 8

The document outlines the implementation of SQL joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, using sample tables for demonstration. It provides SQL queries for each type of join along with their expected outputs. Additionally, it includes viva questions related to joins, their importance, and differences between various types.

Uploaded by

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

DBMS Manual-Exp - 8

The document outlines the implementation of SQL joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, using sample tables for demonstration. It provides SQL queries for each type of join along with their expected outputs. Additionally, it includes viva questions related to joins, their importance, and differences between various types.

Uploaded by

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

Global Institute of Technology

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment -8

Aim- Write the queries to implement the joins.

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);

INSERT INTO basket_ a (id, fruit)VALUES(1, 'Apple');

INSERT INTO basket_a (id, fruit)VALUES (2, 'Orange');

INSERT INTO basket_a (id, fruit)VALUES(3, 'Banana');

INSERT INTO basket_a (id, fruit)VALUES (4, 'Cucumber');

CREATE TABLE basket_b (id INT PRIMARY KEY, fruit VARCHAR (100) NOT NULL);

INSERT INTO basket_b (id, fruit)VALUES(1, 'Orange');

INSERT INTO basket_b (id, fruit)VALUES(2, 'Apple');

INSERT INTO basket_b (id, fruit)VALUES(3, Watermelon');

INSERT INTO basket_b (id, fruit)VALUES(4, 'Pear');

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

3 Banana null Null

4 Cucumber null Null

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

null Null 3 Watermelon

null Null 4 Pear

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

3 banana null Null

4 cucumber null Null

null null 3 Watermelon

null null 4 Pear

DR SANGEETA SONI 40
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

VIVA OUESTIONS-

1. What is a Join? List its different types.


Specifically, there are four types of joins as follows:

 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.

2. What is the difference between a LEFT JOIN and a RIGHT JOIN?

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.

3. Why are joins important in SQL management?

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.

4. What is a CROSS JOIN?

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.

5. Is it possible to join a SQL table to itself?

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.

6. What is the difference between FULL JOIN and CROSS JOIN?

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

You might also like