0% found this document useful (0 votes)
6 views25 pages

22 5cosc020w Lect06 SQL Join

The document outlines Lecture 06 of a database systems course, focusing on SQL join queries for retrieving records from multiple tables. It covers various types of joins including inner, left-outer, right-outer, and full-outer joins, along with examples of SQL statements for creating and populating tables. Additionally, it discusses the use of ON and WHERE clauses in join statements and provides references for further reading.

Uploaded by

himarajoseph17
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)
6 views25 pages

22 5cosc020w Lect06 SQL Join

The document outlines Lecture 06 of a database systems course, focusing on SQL join queries for retrieving records from multiple tables. It covers various types of joins including inner, left-outer, right-outer, and full-outer joins, along with examples of SQL statements for creating and populating tables. Additionally, it discusses the use of ON and WHERE clauses in join statements and provides references for further reading.

Uploaded by

himarajoseph17
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/ 25

5COSC020W DATABASE SYSTEMS – LECTURE 06

Database Querying – Join Queries


Retrieving records from multiple tables in SQL

Dr Francois ROUBERT
[email protected]

1
Lecture 06 – Outline

oSQL to create and populate tables.


oJoin statements in SQL to query multiple tables.
oJoin statements with ON clause and WHERE clause.
oJoin statements with additional conditions.
oSelf-join statements.
oThree-way join statements.
oInner Join statements.
oLeft-Outer join statements.
oRight-Outer join statements.
oFull-Outer Join using UNION. 2
SQL Statements and subsets of SQL
SELECT
INSERT
UPDATE Data Manipulation Language (DML)
DELETE
MERGE
CREATE
ALTER
DROP
RENAME Data Definition Language (DDL)
TRUNCATE
COMMENT
GRANT
REVOKE Data Control Language (DCL)
COMMIT
ROLLBACK Transaction Control
SAVEPOINT 3
Logical ERD

oEmp has a recursive relationship.


oBetween Dept and Emp
• One-to-Many relationship optional on both sides.
• Dept is the Parent table, Emp is the Child table.
• Child Emp carries a FK deptNo that references the PK deptNo of
its parent Dept.
• Participations are both 0.
oBetween Emp and Device
• One-to-Many relationship optional on one side.
• Emp is the Parent table, Device is the Child table.
• Child Device carries a FK empId that references the PK empId of
its parent Emp.
• Participation is 1 on the Emp side.
• Participation is 1 on the Device side. 4
Creating Dept table
DROP TABLE IF EXISTS Device;
DROP TABLE IF EXISTS Emp;
DROP TABLE IF EXISTS Dept;
CREATE TABLE Dept
(
deptNo INTEGER,
dName VARCHAR(20) UNIQUE NOT NULL,
dLoc VARCHAR(30) NOT NULL,
createDate DATE,
CONSTRAINT d_dno_pk PRIMARY KEY (deptNo)
);
5
Creating Emp table
CREATE TABLE Emp
(
empId INTEGER,
fName VARCHAR(50) NOT NULL,
lName VARCHAR(50) NOT NULL,
position VARCHAR(50) NOT NULL,
hireDate DATE NOT NULL,
salary DECIMAL(7,2) NOT NULL,
commPct DECIMAL(2,2),
email VARCHAR(100) UNIQUE NOT NULL,
deptNo INTEGER,
mgrId INTEGER,
CONSTRAINT e_eid_pk PRIMARY KEY (empId),
CONSTRAINT e_dno_fk FOREIGN KEY (deptNo) REFERENCES Dept(deptNo),
CONSTRAINT e_mid_fk FOREIGN KEY (mgrId) REFERENCES Emp(empId)
); 6
Creating Device table

CREATE TABLE Device


(
deviceNo INTEGER,
deviceMake VARCHAR(50) NOT NULL,
deviceModel VARCHAR(50) NOT NULL,
devicePurchDate DATE NOT NULL,
devicePrice DECIMAL(7,2) NOT NULL,
deviceFaults VARCHAR(200),
empId INTEGER NOT NULL,
CONSTRAINT d_dno_pk PRIMARY KEY (deviceNo),
CONSTRAINT d_eid_fk FOREIGN KEY (empId) REFERENCES Emp(empId)
);

7
Populating Dept tables

INSERT INTO
Dept (deptNo, dName, dLoc, createDate)
VALUES
(10, 'Database Management', 'New Cav', '2021-03-21'),
(20, 'Systems Design', 'Harrow', '2021-01-06'),
(30, 'IT Development', 'New Cav', '2021-04-25'),
(40, 'Project Management', 'Regents', '2021-02-12'),
(50, 'Systems Testing', 'Marylebone', NULL);

8
Populating Emp tables

INSERT INTO
Emp (empId, fName, lName, position, hireDate, salary, commPct, email, deptNo, mgrId)
VALUES
(101, 'Joe', 'Bloggs', 'Project Manager', '2021-03-01', 5200.00, 0.25, '[email protected]', 40, NULL),
(102, 'Jim', 'Marts', 'Database Admin', '2021-03-22', 4400.00, NULL, '[email protected]', 10, 101),
(103, 'Jen', 'Fonts', 'Python Developer', '2021-04-28', 4800.00, 0.12, '[email protected]', 30, 101),
(104, 'Jon', 'Pop', 'Database Architect', '2021-03-23', 4000.00, NULL, '[email protected]', 10, 102),
(105, 'Tom', 'Dogs', 'UI Designer', '2021-01-10', 4000.00, NULL, '[email protected]', 20, 102),
(106, 'Tek', 'Roggs', 'Project Manager', '2021-01-01', 5200.00, 0.13, '[email protected]', 40, NULL),
(107, 'Tim', 'Clogs', 'Java Developer', '2021-04-29', 4300.00, 0.15, '[email protected]', NULL, 105),
(108, 'Tam', 'Kelps', 'UX Designer', '2021-06-15', 3900.00, NULL, '[email protected]', NULL, 106);

9
Populating Device tables

INSERT INTO
Device
(deviceNo, deviceMake, deviceModel, devicePurchDate, devicePrice, deviceFaults, empId)
VALUES
(751, 'Pear Phone', 'P Phone 13', '2021-01-12', 976.00, NULL, 106),
(752, 'Pear Tablet', 'P Tablet 04', '2021-01-12', 854.00, NULL, 106),
(753, 'Space Laptop', 'S PC 11', '2021-01-13', 1231.00, 'Scratched', 106),
(754, 'Space Phone', 'S Phone 11', '2021-03-23', 887.00, NULL, 101),
(755, 'Pear Tablet', 'P Tablet 05', '2021-03-23', 1045.00, NULL, 101),
(756, 'Pear Phone', 'P Phone 13', '2021-04-02', 976.00, NULL, 102),
(757, 'Pear Phone', 'P Phone 13', '2021-04-01', 976.00, NULL, 104),
(758, 'Pear Phone', 'P Phone 13', '2021-02-12', 976.00, 'Slow', 105),
(759, 'Space Laptop', 'S PC 54', '2021-02-13', 1143.00, NULL, 105),
(760, 'Space Tablet', 'S Tab 2312', '2021-02-15', 998.00, NULL, 105),
(761, 'Move Phone', 'M Phone 32', '2021-05-07', 1241.00, 'Crashes', 103),
(762, 'Move Laptop', 'M Laptop 3223', '2021-05-08', 1678.00, NULL, 103);

10
Join statement using ON clause (without prefixes)
oJoin statement to retrieve data from 2 tables.
oIt retrieves records in parent table with matching records in child table.
oRecords in parent table without matching records in child table not added.
oRecords in child table without matching records in parent table not added.

SELECT dName, dLoc, lName, position, email


FROM Dept JOIN Emp
ON Dept.deptNo = Emp.deptNo;

Parent.PK = Child.FK
11
Join statement using ON clause (with prefixes)
oWith prefixes (preferred).

SELECT Dept.dName, Dept.dLoc, Emp.lName, Emp.email


FROM Dept JOIN Emp
ON Dept.deptNo = Emp.deptNo;

oPrefix required if retrieving the Parent’s PK/Child’s FK as ambiguous.

SELECT Dept.deptNo, Dept.dName, Emp.lName,Emp.email


FROM Dept JOIN Emp
ON Dept.deptNo = Emp.deptNo;

Parent.PK = Child.FK 12
Join statement using ON clause (with prefixes & aliases)

oWith prefixes and with aliases (best one!)

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D JOIN Emp E
ON D.deptNo = E.deptNo;

Parent.PK = Child.FK

13
Join statement using WHERE clause (with prefixes & aliases)

oWith prefixes and with aliases

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D, Emp E
WHERE D.deptNo = E.deptNo;

Parent.PK = Child.FK

14
Join statement using ON clause with additional condition

oAdditional condition to restrict retrieved records

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D JOIN Emp E
ON D.deptNo = E.deptNo
AND D.dName LIKE '%Database%';

Parent.PK = Child.FK
15
Join condition using WHERE clause with additional condition

oAdditional condition to restrict retrieved records

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D, Emp E
WHERE D.deptNo = E.deptNo
AND D.dName LIKE '%Database%';

Parent.PK = Child.FK
16
Join condition using ON clause with 2 additional conditions

oUse brackets to separate conditions

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D JOIN Emp E
ON D.deptNo = E.deptNo
AND (D.dName LIKE '%Database%'
OR E.salary < 4700);

Parent.PK = Child.FK
17
Self-Join statement

oAliases are required to distinguish between the uses of the same table!

SELECT M.empId, M.lName, M.fName,


E.empId, E.lName, E.fName
FROM Emp M JOIN Emp E
ON M.empId = E.mgrId;

Parent.PK = Child.FK
18
Three-way join statement

oJoin statement to retrieve data from 3 tables

SELECT D.dName, D.dLoc, D.createDate,


E.lName, E.fName, E.email,
DV.deviceMake, DV.deviceModel, DV.deviceFaults
FROM Dept D
JOIN Emp E ON D.deptNo = E.deptNo
JOIN Device DV ON E.empId = DV.empId;

Parent.PK = Child.FK
19
Inner-Join statement

oJoin statement retrieves records with matching values in both tables!


oSometimes referred to as inner-join

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D JOIN Emp E
ON D.deptNo = E.deptNo;

Parent.PK = Child.FK

20
Left-Outer Join

oLeft-Outer Join retrieves records with matching values in both tables


PLUS records from table on the LEFT without matching records on the
table on the RIGHT (of the JOIN clause).
oIt puts NULL values in the fields without matching values.

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D LEFT OUTER JOIN Emp E
ON D.deptNo = E.deptNo;

Parent.PK = Child.FK
21
Right-Outer Join condition using ON clause

oRight-Outer Join retrieves records with matching values in both tables


PLUS records from table on the RIGHT without matching records on the
table on the LEFT (of the JOIN clause).
oIt puts NULL values in the fields without matching values.

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D RIGHT OUTER JOIN Emp E
ON D.deptNo = E.deptNo;

Parent.PK = Child.FK
22
Full-Outer Join

oFull-Outer Join retrieves records with matching values in both tables


PLUS records from table on the RIGHT without matching records on the
table on the LEFT (of the JOIN clause)
PLUS records from table on the LEFT without matching records on the
table on the RIGHT (of the JOIN clause)
oIt puts NULL values in the fields without matching values.
oNot supported by MySQL and needs to be written with UNION.

23
Left-Outer Join with UNION

SELECT D.dName, D.dLoc, E.lName, E.email


FROM Dept D LEFT OUTER JOIN Emp E
ON D.deptNo = E.deptNo
UNION
SELECT D.dName, D.dLoc, E.lName, E.email
FROM Dept D RIGHT OUTER JOIN Emp E
ON D.deptNo = E.deptNo;
Parent.PK = Child.FK

UNION ALL can be used instead of UNION to keep duplicate records.


24
References and Essential Readings

oModule Reading List: https://rl.talis.com/3/westminster/lists/2CAA7D6B-


DCAD-AB71-C97B-7FEFCB499C28.html

oConnolly, T. & Begg, C. E. (2015). Database systems: a practical


approach to design, implementation and management. 6th Edition
(Global Edition). Pearson Education.

oElmasri, R. & Navathe, S. (2017). Fundamentals of Database Systems.


7th Edition (Global Edition). Pearson Education.

oW3Schools (2022). SQL Tutorial. Available from


https://www.w3schools.com/sql/ [Accessed 15 September 2022]
25

You might also like