Lec11 - Lab - CSC371 - Database Systems
Lec11 - Lab - CSC371 - Database Systems
(Lab)
(Spring2020)
Abdul Qayyum [email protected]
Samia Arshad [email protected]
Faisal Mumtaz [email protected]
1
Previous Lecture Review
SQL Joins
Where clause
Join & ON
INNER JOIN
ALIAS for table name
Join two tables
Join three or more tables
2
3
Agenda
SQL Joins
Outer Joins
Left Outer Join
Right Outer Join
Full Outer Join
Self Join
4
SQL INNER JOIN
The INNER JOIN keyword selects records that have matching values in both
tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
5
select * from branch inner join staff on branch.branchno = staff.branchno
6
select * from staff inner join branch on branch.branchno = staff.branchno
7
Outer Joins
8
SQL LEFT OUTER JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from
the right table (table2). The result is NULL from the right side, if there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
10
Generate a report for total salary of
each branch
select branch.branchno, branch.city, sum(staff.salary) As [Branch Salary]
from branch left join staff on Branch.branchno = staff.branchno
Group by branch.branchNo,branch.city
order by branchno
11
SQL RIGHT OUTER JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1). The result is NULL from the left side,
when there is no match.
12
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SG5 13
SL21
Detail of all properties with the detail of staff
who assigned each property
select staff.staffno, staff.fname,staff.position, propertyforrent.*
from staff right join PropertyForRent on staff.staffno =PropertyForRent.staffno
order by propertyforrent.propertyNo
14
SQL FULL OUTER JOIN
The FULL OUTER JOIN keyword returns all records when there is a match in
left (table1) or right (table2) table records.
Note: FULL OUTER JOIN can potentially return very large result-sets!
Tip: FULL OUTER JOIN and FULL JOIN are the same.
15
16
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
select * from staff full join PropertyForRent on staff.staffno = PropertyForRent.staffno
17
Is there any staff number who did not assign a
property and there is any property which is not
assigned to any staff number at branch B003
select staff.staffno, staff.fname, propertyforrent.propertyNo, PropertyForRent.branchno
from staff full join PropertyForRent on staff.staffno = PropertyForRent.staffno
where propertyforrent.branchno='B003' or staff.branchno ='B003'
18
select staff.staffno, staff.fname, propertyforrent.propertyNo, PropertyForRent.branchno
from staff full join PropertyForRent on staff.staffno = PropertyForRent.staffno
where propertyforrent.branchno='B003’
19
Self Join
Used when we want to join a table with the same table
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
20
We want to pair Managers and Assistants as teams, so need data to tie them up
21
Reading Assignment
Equi-join
Non Equi-join
Natural Join
Semi join
Theta join
22
Summary
SQL Joins
Outer Joins
Left Outer Join
Right Outer Join
Full Outer Join
Self Join
23