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

Lec11 - Lab - CSC371 - Database Systems

This document discusses different types of SQL joins, including inner joins, outer joins (left, right, and full), and self joins. Inner joins return records that match between both tables, while outer joins return all records from one table and matched records from the other table. Self joins allow a table to join with itself. Examples of join queries are provided to retrieve data from multiple tables.

Uploaded by

sp22-bse-097
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lec11 - Lab - CSC371 - Database Systems

This document discusses different types of SQL joins, including inner joins, outer joins (left, right, and full), and self joins. Inner joins return records that match between both tables, while outer joins return all records from one table and matched records from the other table. Self joins allow a table to join with itself. Examples of join queries are provided to retrieve data from multiple tables.

Uploaded by

sp22-bse-097
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CSC371-Database Systems I Lecture-11

(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

 Left Outer Join


 Right Outer Join
 Full Outer Join

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;

SELECT Branch.branchno, Branch.city, staff.staffno, staff.fname


FROM Branch
LEFT JOIN Staff ON Branch.branchno = staff.branchno
ORDER BY branch.branchno;

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;

 select * from staff right join PropertyForRent on staff.staffno = PropertyForRent.staffno

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

 SELECT s1.staffno, s1.position, s2.staffno,s2.position, s1.branchno


 FROM staff s1, staff s2
 WHERE s1.position != s2.position
AND s1.position = 'manager’ AND s2.position = 'assistant’
order by s1.branchno

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

You might also like