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

Lec10 - Lab - CSC371 - Database Systems

This document discusses SQL joins. It explains that joins are used to combine rows from two or more tables based on a common column between them. It provides examples of inner joins, joining more than two tables, and using aliases with table names. It also summarizes that inner joins select records that have matching values in both tables based on the join condition.

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)
10 views

Lec10 - Lab - CSC371 - Database Systems

This document discusses SQL joins. It explains that joins are used to combine rows from two or more tables based on a common column between them. It provides examples of inner joins, joining more than two tables, and using aliases with table names. It also summarizes that inner joins select records that have matching values in both tables based on the join condition.

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/ 21

CSC371-Database Systems I Lecture-10

(Lab)
(Spring2020)
Abdul Qayyum [email protected]
Samia Arshad [email protected]
Faisal Mumtaz [email protected]

1
Previous Lecture Review

 SQL Group By Clause


 SQL Having Clause
 SQL Views

2
 CREATE TABLE Branch(
 branchNo VARCHAR(15) NOT NULL,
 street VARCHAR(20) NOT NULL,
 city VARCHAR(20) NOT NULL,
 postcode VARCHAR(15) NOT NULL,
 CONSTRAINT pk_branches PRIMARY KEY(branchNo)
 );
 CREATE TABLE Staff(
 staffNo VARCHAR(10) NOT NULL
 fName VARCHAR(15) NOT NULL,
 lName VARCHAR(15) NOT NULL,
 position VARCHAR(15) NOT NULL,
 sex VARCHAR(15) NOT NULL,
 DOB DATE NOT NULL,
 salary INT(15) NOT NULL,
 branchNo VARCHAR(15) NOT NULL,
 CONSTRAINT pk_staffs PRIMARY KEY(staffNo),
 CONSTRAINT fk_staffs FOREIGN KEY(branchNO) REFERENCES Branch(branchNo)
 );
3
 CREATE TABLE PrivateOwner(
ownerNo VARCHAR(15) NOT NULL,
fName VARCHAR(15) NOT NULL,
lName VARCHAR(15) NULL,
address VARCHAR(50) NOT NULL,
telNo VARCHAR(15) NOT NULL,
eMail VARCHAR(20) NOT NULL,
CONSTRAINT pk_privateOwners PRIMARY KEY(ownerNo)
);

4
 CREATE TABLE PropertyForRent(
propertyNo VARCHAR(15) NOT NULL,
street VARCHAR(20) NOT NULL,
city VARCHAR(20) NULL,
postcode VARCHAR(15) NOT NULL,
TYPE VARCHAR(15) NOT NULL,
rooms INT NOT NULL,
rent INT NOT NULL,
ownerNo VARCHAR(15) NOT NULL,
staffNo VARCHAR(15) NULL,
branchNo VARCHAR(10) NOT NULL,
CONSTRAINT pk_propertyForRents PRIMARY KEY(propertyNo),
CONSTRAINT fk_propertyForRents FOREIGN KEY(ownerNO) REFERENCES PrivateOwner(ownerNo),
CONSTRAINT fk_property FOREIGN KEY(staffNo) REFERENCES Staff(staffNo),
CONSTRAINT fk_for FOREIGN KEY(branchNo) REFERENCES Branch(branchNo)
);
5
 INSERT INTO Branch VALUES ('B005', '22 Deer Rd', 'London', 'SW1 4EH');
 INSERT INTO Branch VALUES ('B007', '16 Argyll St', 'Aberdeen', 'AB2 3SU');
 INSERT INTO Branch VALUES ('B003', '163 Main St', 'Glasgow', 'G11 9QX');
 INSERT INTO Branch VALUES ('B004', '32 Manse Rd', 'Bristol', 'BS99 1NZ');
 INSERT INTO Branch VALUES ('B002', '56 Clover Dr', 'London', 'NW10 6EU');

 INSERT INTO Staff VALUES ('SL21', 'John', 'White','Manager', 'M', '1945-10-01', 30000, 'B005');
 INSERT INTO Staff VALUES ('SG37', 'Ann', 'Beech','Assistant', 'F', '1960-10-11', 12000, 'B003');
 INSERT INTO Staff VALUES ('SG14', 'David','Ford', 'Supervisor','M', '1958-11-24', 18000, 'B003');
 INSERT INTO Staff VALUES ('SA9', 'Mary', 'Howe', 'Assistant', 'F', '1970-02-19', 9000, 'B007');
 INSERT INTO Staff VALUES ('SG5', 'Susan','Brand','Manager', 'F', '1940-06-03', 24000, 'B003');
 INSERT INTO Staff VALUES ('SL41', 'Julie','Lee', 'Assistant', 'F', '1965-06-13', 9000, 'B005');

6
 INSERT INTO PrivateOwner VALUES ('CO46', 'Joe', 'Keogh', '2 Fergus Dr, Aberdeen AB2 7SX','01224-861212' ,'[email protected]');
 INSERT INTO PrivateOwner VALUES ('CO87', 'Carol','Farrel','6 Achray St, Glasgow G32 9DX', '0141-357-7419','[email protected]');
 INSERT INTO PrivateOwner VALUES ('CO40', 'Tina', 'Murphy','63 Well St, Glasgow G42', '0141-943-1728','[email protected]');
 INSERT INTO PrivateOwner VALUES ('CO93', 'Tony', 'Shaw', '12 Park Pl, Glasgow G4 0QR', '0141-225-7025','[email protected]');

 INSERT INTO PropertyForRent VALUES ('PA14', '16 Holhead', 'Aberdeen', 'AB7 5SU', 'House',6, 650, 'CO46', 'SA9', 'B007');
 INSERT INTO PropertyForRent VALUES ('PL94', '6 Argyll St', 'London', 'NW2', 'Flat', 4, 400, 'CO87', 'SL41', 'B005');
 INSERT INTO PropertyForRent VALUES ('PG4', '6 Lawrence St','Glasgow', 'G11 9QX', 'Flat', 3, 350, 'CO40', NULL, 'B003');
 INSERT INTO PropertyForRent VALUES ('PG36', '2 Manor Rd', 'Glasgow', 'G32 4QX', 'Flat', 3, 375, 'CO93', 'SG37', 'B003');
 INSERT INTO PropertyForRent VALUES ('PG21', '18 Dale Rd', 'Glasgow', 'G12', 'House',5, 600, 'CO87', 'SG37', 'B003');
 INSERT INTO PropertyForRent VALUES ('PG16', '5 Novar Dr', 'Glasgow', 'G12 9AX', 'Flat', 4, 450, 'CO93', 'SG14', 'B003');

7
8
Agenda
SQL Joins

9
SQL JOIN
 Used to combine rows from two or more tables, based on a reference column between them.
Table Joining using Where Clause
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2

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


FROM Branch, Staff
Where Branch.branchno = Staff.branchno;

11
Alternative to where for Joining tables
SELECT *
FROM table1 JOIN table2……
[ON (join_condition)]

 SELECT Staff.staffno, Staff.fname ,Staff.branchno, Branch.city


FROM Branch join Staff
ON Branch.branchno = Staff.branchno;

12
 Joining More than Two Tables
 select staff.staffno, staff.fname, propertyforrent.propertyno,
privateowner.ownerno, privateowner.fname
From Staff,PropertyForRent, PrivateOwner
Where staff.staffno = propertyforrent.staffno
AND propertyforrent.ownerno = privateowner.ownerno;

13
 Joining More than Two Tables Alternative Approach
 select staff.staffno, staff.fname, propertyforrent.propertyno,
privateowner.ownerno, privateowner.fname
From Staff Join PropertyForRent
ON staff.staffno = propertyforrent.staffno
Join PrivateOwner
ON propertyforrent.ownerno = privateowner.ownerno;

14
Can add condition
 select staff.staffno, staff.fname, propertyforrent.propertyno,
privateowner.ownerno, privateowner.fname
From Staff, ProperForRent, PrivateOwner
Where staff.staffno = propertyforrent.staffno
AND propertyforrent.ownerno = privateowner.ownerno
AND Privateowner.fname <> ‘Joe’

15
Use of Alias with Table name
 select S.staffno, S.fname, P.propertyno, O.ownerno, O.fname
From Staff S, ProperForRent P, PrivateOwner O
Where S.staffno = P.staffno
AND P.ownerno = O.ownerno
AND O.fname <> ‘Joe’

16
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;`

17
 SELECT Staff.staffno, Staff.fname ,Staff.branchno, Branch.city
FROM Branch inner join Staff
ON Branch.branchno = Staff.branchno;

18
We need to retrieve data of staff name their
numbers, their city where they are working
along with their branch number.
SELECT Staff.staffno, Staff.fname ,Staff.branchno, Branch.city
FROM Branch
INNER JOIN Staff
ON Branch.branchno = Staff.branchno;

19
Join three or more tables
 select staff.staffno, staff.fname, propertyforrent.propertyno, privateowner.ownerno,
privateowner.fname
 From ((Staff
 inner join propertyforrent on staff.staffno = propertyforrent.staffno)
 inner join privateowner on propertyforrent.ownerno = privateowner.ownerno);

20
Summary

 SQL Joins

21

You might also like