Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Database and Management System Lab Report: 7 SQL Joins Submitted To

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Database and Management System

Lab Report : 7
SQL Joins
Submitted To:
Sir Shahid Ali
Submitted By:
Muhammad Shoaib Nadeem
16-CP-76
Section :
Omega
Date:
23/3/2019
Department Of Computer Engineering
UET Taxila
Lab Objectives:
The SQL Joins clause is used to combine records from two or more tables in a database.
Lab Tasks:

1): Consider the following two tables:

a) Persons table is as follow:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

a) ORDER table is as follow:

O_Id OrderDate OrderPrice Per_id

1 2008/11/12 1000 3

2 2008/10/23 1600 3

3 2008/09/02 700 1

4 2008/09/03 300 1

5 2008/08/30 2000 NULL

Person Table:

QUERY

create table Persons_7( P_ID int ,


LastName nvarchar(10),
FirstName nvarchar(10),
Addresss nvarchar(25) ,
City nvarchar(20),
);
insert into Persons_7 values(1,'Hansen','Ola','Timoteivn 10','Sandness');

insert into Persons_7 values(2,'Sayendon','Tove','Borgvn 23','Sandness');

insert into Persons_7 values(3,'petterson','Kari','Storgt 20', 'Stavanger');


Select * From Persons_7

Output

Order Table

QUERY
Create Table Orders_7(
Order_id int not null primary key,
OrderDate date,
OrderPrice int,
Per_ID int,
);

insert into Orders_7 values(1, '2008-11-12',1000,3);


insert into Orders_7 values(2,'2008/10/23',1600,3);
insert into Orders_7 values(3,'2008/09/02',700,1);
insert into Orders_7 values(4,'2008/09/03',300,1);
insert into Orders_7 values(5,'2008/08/30',2000,Null);

select * from Orders_7

Output

• Apply inner Join on above tables and show the result

Query

SELECT Persons_7.LastName, Persons_7.FirstName, Orders_7.Order_id, Orders_7.OrderPrice


FROM Persons_7
INNER JOIN Orders_7
ON Persons_7.P_ID=Orders_7.Per_ID ORDER BY Persons_7.FirstName
Output

Apply Left Join (or left outer join) on above tables and show the result.

Query

SELECT Persons_7.LastName, Persons_7.FirstName, Orders_7.Order_id FROM Persons_7


LEFT JOIN Orders_7
ON Persons_7.P_Id=Orders_7.Per_ID ORDER BY Persons_7.LastName

Output

Apply Right Join (or right outer join) on above tables and show the result.

Query

SELECT Persons_7.LastName, Persons_7.FirstName, Orders_7.Order_id , Orders_7.OrderPrice


From Persons_7
Right JOIN Orders_7
ON Persons_7.P_Id=Orders_7.Per_ID ORDER BY Persons_7.LastName

Output
Apply Full Join (or Full outer join) on above tables and show the result.

Query

SELECT Persons_7.LastName, Persons_7.FirstName, Orders_7.Order_id , Orders_7.OrderPrice


From Persons_7
FULL JOIN Orders_7
ON Persons_7.P_Id=Orders_7.Per_ID ORDER BY Persons_7.LastName

Output

Summary
The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is
a means for combining fields from two tables by using values common to each.

There are different types of joins available in SQL −


 INNER JOIN − returns rows when there is a match in both tables.

 LEFT JOIN − returns all rows from the left table, even if there are no matches in the right table.

 RIGHT JOIN − returns all rows from the right table, even if there are no matches in the left table.

 FULL JOIN − returns rows when there is a match in one of the tables.

 SELF JOIN − is used to join a table to itself as if the table were two tables, temporarily renaming at
least one table in the SQL statement.

 CARTESIAN JOIN − returns the Cartesian product of the sets of records from the two or more joined
tables.

You might also like