22 5cosc020w Lect06 SQL Join
22 5cosc020w Lect06 SQL Join
Dr Francois ROUBERT
[email protected]
1
Lecture 06 – Outline
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.
Parent.PK = Child.FK
11
Join statement using ON clause (with prefixes)
oWith prefixes (preferred).
Parent.PK = Child.FK 12
Join statement using ON clause (with prefixes & aliases)
Parent.PK = Child.FK
13
Join statement using WHERE clause (with prefixes & aliases)
Parent.PK = Child.FK
14
Join statement using ON clause with additional condition
Parent.PK = Child.FK
15
Join condition using WHERE clause with additional condition
Parent.PK = Child.FK
16
Join condition using ON clause with 2 additional conditions
Parent.PK = Child.FK
17
Self-Join statement
oAliases are required to distinguish between the uses of the same table!
Parent.PK = Child.FK
18
Three-way join statement
Parent.PK = Child.FK
19
Inner-Join statement
Parent.PK = Child.FK
20
Left-Outer Join
Parent.PK = Child.FK
21
Right-Outer Join condition using ON clause
Parent.PK = Child.FK
22
Full-Outer Join
23
Left-Outer Join with UNION