Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Data Retrieval (2) Topic 5 - 5.

Scope and Coverage


This topic will cover:
• Referential integrity in relational databases
• Types of joins
• Retrieving data using joins
• Retrieving data using sub-queries

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.2

Learning Outcomes
By the end of this topic students will be able to:
• Outline the concept of referential integrity and say
why it is important in a relational database
• Understand how to retrieve data from one or more
tables using join
• Understand how to retrieve data from one or more
tables using sub-queries

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.3

Referential Integrity
• A foreign key must refer to a candidate key (usually
a primary key) elsewhere in the database or must
be null.

• Why is this important?

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.4

Student ID Name Address Course

Keys - 1… 28 Guy Smith History

23 Sarah 12 New Street Computing


Anusiem Lagos

Module Code Name


Primary keys Med1 Medieval History 1
OS Operating Systems
Student ID Modules Code
Med2 Medieval History 2
28 Net
NET Networks
23 Med 1
TCE Twentieth Century
28 OS History

23 Med 2

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.5

Keys - 2…
Student ID Name Address Course

28 Guy Smith History

23 Sarah 12 New Street Computing


Foreign keys Anusiem Lagos

Student ID Modules Code

28 Net Module Code Name


Med1 Medieval History 1
23 Med 1
OS Operating Systems
28 OS
Med2 Medieval History 2
23 Med 2
NET Networks
TCE Twentieth Century
History

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.6

Recap – Parts of a Select Statement


What does ‘Select’ do

Select branchID, Count(staff_id) What is the


‘From’ doing?
From workers
What is the ‘Where’
Where branchType = ‘Main’ soing?
Group by branchID
Groups by some column
Having Count (staff_id) > 1 Value
Order by branchID
What does ‘Having’
What does the ‘Order by’ do? do?

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.7

What columns will appear

Select branchID, Count(staff_id)


Which table
From workers
Where branchType = ‘Main’ Condition

Group by branchID Groups by some column


Having Count (staff_id) > 1 Value

Order by branchID
Restricts what will
Specifies the order of the result Be grouped

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.8

Two Tables
Create table departments
(dept_no number(5 ) not null,
department_name varchar(30),
location varchar2(3)
primary key dept_no);

Create table workers


(emp_no number(5) not null,
first_name varchar(30),
last_name varchar(30),
job_title varchar(30),
age number(3),
dept_no number(5),
primary key emp_no,
foreign key (dept_no) references departments)

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.9

Joining Tables in an SQL Query


Select d.department_name, w.first_name,
w.last_name, w.job_title
from departments d, workers w
where d.dept_no = w.dept_no;

What does this query do?

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.10

The Join Operation


• Used to retrieve data from more than one table

• Simple join

• Multi-table join

• Outer-join

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.11

Simple Join on Two Tables


Select d.department_name, d.location,
w.first_name, w.last_name, w.job_title
from departments d, workers w
where d.dept_no = w.dept_no
and d.location = 'Cairo';

•Use the attribute that connects the tables in the


database. In this case ‘dept_no’.

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.12

Joining more than two Tables


Select d.department_name, d.location,
w.first_name, w.last_name, w.job_title, j.salary
from departments d, workers w, jobs
where d.dept_no = w.dept_no
and w.job_title = j.job_title;

•Retrieves the salary from a separate table jobs


which is linked via job_title

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.13

Outer Join
• Derives from an operation in relational algebra

• When joining two tables what happens if there is no


matching row in one of the tables.

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.14

EmployeeID Name DeptNo


1 Ron Howard 1
2 Satpal Singh 3
5 Arfir Rahman
7 Jody Kodogo 7

DeptNo DeptName
1 History
3 English Literature
7 Mathematics

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.15

EmployeeID Name DeptNo


1 Ron Howard 1
2 Satpal Singh 3
5 Arfir Rahman
7 Jody Kodogo 7

DeptNo DeptName
1 History
3 English Literature
7 Mathematics
33 Chemistry

Select E.employeeID, D.DeptNo, D.DeptName


From Employees E, Departments D
Where E.deptNo = D.deptNo;
What will be the result of this query?

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.16

EmployeeID Name DeptNo Select E.employeeID, D.DeptNo,


1 Ron Howard 1 D.DeptName
From Employees E, Departments D
2 Satpal Singh 3
Where E.deptNo = D.deptNo;
5 Arfir Rahman
7 Jody Kodogo 7

DeptNo DeptName
1 History
3 English Literature
7 Mathematics
33 Chemistry

EmployeeID DeptNo DeptName


1 1 History
2 3 English Literature
7 7 Mathematics

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.17

Types of Outer Join


• LEFT OUTER JOIN – all the rows from the first (left)
table that are unmatched in the second (right) table.

• RIGHT OUTER JOIN – all the of the second (right)


table that hare unmatched rows in the first (left)
table

• FULL OUTER JOIN – all unmatched rows.

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.18

Left Outer Join


Select E.employeeID, D.DeptNo, D.DeptName
From Employees E LEFT JOIN Departments D
ON E.deptNo = D.deptNo;

EmployeeID DeptNo DeptName


1 1 History
2 3 English Literature
5
7 7 Mathematics

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.19

Right Outer Join


Select E.employeeID, D.DeptNo, D.DeptName
From Employees E RIGHT JOIN Departments D
ON E.deptNo = D.deptNo;

EmployeeID DeptNo DeptName


1 1 History
2 3 English Literature
7 7 Mathematics
33 Chemistry

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.20

Full Outer Join


Select E.employeeID, D.DeptNo, D.DeptName
From Employees E LEFT JOIN Departments D
ON E.deptNo = D.deptNo;

EmployeeID DeptNo DeptName


1 1 History
2 3 English Literature
5
7 7 Mathematics
33 Chemistry

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.21

Sub-queries
Select d.department_name, d.location
From departments d, workers w
Where d.dept_no = w.dept_no
And w.age =
(select max(w2.age)
From workers w2);

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.22

Correlated Sub-query
SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id
FROM order_items o
WHERE o.product_id = p.product_id);

•This will select product_names from products


where all the products exist on a table called
older_items.

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.23

Learning Outcomes
By the end of this unit students will be able to:
• Outline the concept of referential integrity and say
why it is important in a relational database
• Understand how to retrieve data from one or more
tables using join
• Understand how to retrieve data from one or more
tables using sub-queries
Have we met them?

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.24

References
• Connolly and Begg, Database Systems a Practical
Approach to Design, implementation and Management
Chapters 4 and 5 Fourth Edition
• Benyon-Davis, Database Systems. Chapters 12 and13
Third Edition
• Dietrich, Suzanne W, Understanding Relational
Database Query Languages Chapter 5 1st Edition 2001
• Beginner SQL Tutorial:
http://beginner-sql-tutorial.com/sql-subquery.htm
Retrieved 01-June-2011

V1.0 © NCC Education Limited


Data Retrieval (2) Topic 5 - 5.25

Topic 5 – Data Retrieval (2)

Any Questions?

V1.0 © NCC Education Limited

You might also like