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

Implementation of Different Types of Joins

for veltech students
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)
25 views

Implementation of Different Types of Joins

for veltech students
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/ 9

Right side Contents

Task 5- Implementation of different types of Joins


Aim: To Perform the advanced query processing and test its heuristics using the designing of
optimizing complex queries and their equivalence queries
Procedures:
Types of JOIN
1. Simple Join
1. Equi-join :
2. NonEqui-join :
2. Self-Join Query
3. Outer Join
1. Inner Join
2. Left Outer Join
3. Right Outer Join
4. Full Outer Join
Theory :
The SQL Joins clause is used to combine records from two or more tables in adatabase. A JOIN is
a means for combining fields from two tables by using values common to each. The join is actually
performed by the ‘where’ clause which combines specified rows of tables.
Tables used in this Experiment:
Sailors(sid: integer, sname: string, rating: integer, age: real); Boats(bid: integer, bname: string,
color: string); Reserves(sid: integer, bid: integer, day: date).
Simple Join:
It is the most common type of join. It retrieves the rows from 2 tables
having acommon column and is further classified into
Equi-join :
A join, which is based on equalities, is called equi-join.
Example:
Find all information of sailors who have reserved boat number 103.
SELECT S.* FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 103

In the above statement, R.bid = 101 performs the equalities statement. It retrieves rows from both
the tables provided they both have the same id as specified by the where clause. Since the where
clause uses the comparison operator (=) to perform a join, it is said to be equijoin. It combines the
matched rows of tables. It can be used as follows:

To insert records in the target table.


To create tables and insert records in this table.

To update records in the target table. To create views.

Non Equi-join:
It specifies the relationship between columns belonging to different tables by making use of
relational operators other than’=’.

Example:

Find all information of sailors who have not reserved boat number 103.

SELECT S.*
FROM Sailors S, Reserves R
WHERE S.sid = R.sid AND R.bid < > 103
Table Aliases
Table aliases are used to make multiple table queries shorted and more readable. We givean alias
name to the table in the ‘from’ clause and use it instead of the name throughout the query.

Self join:
Joining of a table to itself is known as self-join. It joins one row in a table to
another. It can compare each row of the table to itself and also with other rows of the same table.
Example:
SELECT *
FROM sailors X, sailors Y
WHERE X.age >= (

);
SELECT AVG ( age) FROM X.sailors;

Outer Join:
▪ It extends the result of a simple join. An outer join returns all the rows returned by
simplejoin as well as those rows from one table that do not match any row from the
table.
▪ The symbol(+) represents outer join.
Outer Join
• Inner Join
• Left Join
• Right Join
• Full Outer Join

• SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
• INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
• JOIN or INNER JOIN
JOIN and INNER JOIN will return the same result.
INNER is the default join type for JOIN, so when you write JOIN the parser actually
writes INNER JOIN.
Left side Contents-Output side:
PRACTICE ASSIGNMENT:
Consider the following schema:
➢ Sailors (sid, sname, rating, age)

➢ Boats (bid, bname, color)

➢ Reserves (sid, bid, day(date))

Sailors Entity Boats Entity

sid bid

sname banme

rating color

age

Reserves Entity

sid

bid

day(date)

Create the sailors Tables:


CREATE TABLE sailors (
sid int not null,
sname varchar(32),
rating int,
age real,
CONSTRAINT PK_sailors PRIMARY KEY (sid)
);
2) Insert Records into sailors table

INSERT INTO sailors( sid, sname, rating, age )VALUES ( 22, 'Dustin', 7, 45.0);
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 29, 'Brutus', 1, 33 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 31, 'Lubber', 8,
55.5);
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 32, 'Andy', 8, 25.5 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 58, 'Rusty', 10, 35 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 64, 'Horatio', 7, 35 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 71, 'Zorba', 10, 16 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 74, 'Horatio', 9, 40 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 85, 'Art', 3, 25.5 );
INSERT INTO sailors( sid, sname, rating, age )VALUES ( 95, 'Bob', 3, 63.5 );

2. Create the boats Tables:


CREATE TABLE boats (bid int not null,
bname varchar(32),
color varchar(32),
CONSTRAINT PK_boats PRIMARY KEY (bid)
);
Insert Records into boats table
INSERT INTO boats ( bid, bname, color ) VALUES ( 101, 'Interlake', 'blue' );

INSERT INTO boats ( bid, bname, color ) VALUES ( 102, 'Interlake', 'red' );

INSERT INTO boats ( bid, bname, color ) VALUES ( 103, 'Clipper', 'green' );

INSERT INTO boats ( bid, bname, color ) VALUES ( 104, 'Marine', 'red' );

3. Create the reserves Tables:


CREATE TABLE reserves (
sid int not null,
bid int not null,
day datetime not null,
CONSTRAINT PK_reserves PRIMARY KEY (sid, bid, day), FOREIGN KEY
(sid) REFERENCES sailors(sid),
FOREIGN KEY (bid) REFERENCES boats(bid)
);
Reserves Table Insert Data
INSERT INTO reserves ( sid, bid, day ) VALUES ( 22, 101, '1998-10-10');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 22, 102, '1998-10-10');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 22, 103, '1998-10-8');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 22, 104, '1998-10-7');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 22, 102, '1998-11-10');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 31, 103, '1998-11-6');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 31, 104, '1998-11-12');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 64, 101, '1998-9-5');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 64, 102, '1998-9-8');
INSERT INTO reserves ( sid, bid, day ) VALUES ( 74, 103, '1998-9-8');

The output table reserves contains the following records:


• sid (sailor ID) ranges from 22 to 74.
• bid (boat ID) ranges from 101 to 104.
• rday (reservation date) is in the format YYYY-MM-DD.
✓ The primary key constraint PK_reserves ensures that each combination of sid,
bid, and rday is unique.
✓ Therefore, duplicate records are not allowed in the table
Inner Join With Two Table
Q1. Find the information of sailors name and reserved boats number using
INNER JOIN.

SELECT sailors.*, reserves.sid FROM sailors INNER JOIN reserves ON


sailors.sid = reserves.sid;
Output:

sid sname rating age


22 Dustin 7 45
22 Dustin 7 45
22 Dustin 7 45
22 Dustin 7 45

Inner Join With Three Table


Q2. Find the name of boat reserved by Lubber
SELECT boats.bname FROM sailors INNER JOIN reserves ON sailors.sid = reserves.sid
AND sailors.sname = 'Lubber' INNER JOIN boats ON reserves.bid = boats.bid;

Output:
bname

Clipper

Marine

Left Outer Join


Q3. Display the sailors name with their reserved boats number using left Outer
Join.
SELECT sailors.*, reserves.* FROM sailors LEFT JOIN reserves ON
sailors.sid = reserves.sid;
Output:
Q4. Display the Sailors reserved Boats date with their sailors name using Right
Outer Join.

SELECT sailors.*, reserves.*


FROM sailors RIGHT JOIN reserves ON
sailors.sid = reserves.sid ;
output:

Result:
Thus the Implementation of different types of Joins was completed successfully

You might also like