Professional Documents
Culture Documents
Relational Algebra
Lecture 03 - Objectives
2
Introduction
Relational algebra and relational calculus are formal
languages associated with the relational model.
– Formally both are equivalent to one another
Relational algebra operations work on one or more
relations to define another relation without changing the
original relations.
Both operands and results are relations, so output from
one operation can become input to another operation.
Allows expressions to be nested, just as in arithmetic.
This property is called closure
Relational Algebra
Five basic operations in relational algebra:
Selection, Projection, Cartesian product, Union,
and Set Difference.
4
Relational Algebra Operations
5
Relational Algebra Operations
6
Sample Database
7
Selection (or Restriction)
predicate (R)
– Works on a single relation R and defines a
relation that contains only those tuples (rows) of
R that satisfy the specified condition (predicate).
8
Example - Selection (or Restriction)
List all staff with a salary greater than £10,000.
9
Task: Selection Operation
branch-name loan-number amount
Downtown L-17 1000
Redwood L-23 2000
Perryridge L-15 1500
Downtown L-14 1500
Mianus L-93 500
Round Hill L-11 900
Perryridge L-16 1300
Query 01: select those tuples of the loan relation where the branch
name is "Perryridge,"
Query 02: find all tuples in which the amount is more than 1200
11
Example - Projection
Produce a list of salaries for all staff, showing only
staffNo, fName, lName, and salary details.
12
Task: Projection Operation
branch-name loan-number amount
Downtown L-17 1000
Redwood L-23 2000
Perryridge L-15 1500
Downtown L-14 1500
Mianus L-93 500
Round Hill L-11 900
Perryridge L-16 1300
Query 01: list all loan numbers and the amount of the loan
loan-number amount
L-17 1000
L-23 2000
L-15 1500
L-14 1500
L-93 500
L-11 900
13
L-16 1300
Task: Selection and Projection Operation
Customer relation
customer-name customer-street customer-city
Jones Main Harrison
Smith North Rye
Hayes Main Harrison
Curry North Rye
Lindsay Park Pittsfield
Turner Putnam Stamford
Williams Nassau Princeton
Adams Spring Pittsfield
Johnson Alma Palo Alto
Glenn Sand Hill Woodside
Brooks Senator Brooklyn
Green Walnut Stamford
Query 01: find the names of those customers who live in Harrison
Cartesian product
RXS
– Defines a relation that is the concatenation of
every tuple of relation R with every tuple of
relation S.
15
Example - Cartesian product
Example - Cartesian product
List the names and comments of all clients who have
viewed a property for rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment
(Viewing))
17
Natural join
R S
– An Equijoin of the two relations R and S over all
common attributes x. One occurrence of each
common attribute is eliminated from the result.
18
Example - Natural join
List the names and comments of all clients who
have viewed a property for rent.
(clientNo, fName, lName(Client))
(clientNo, propertyNo, comment(Viewing))
19
Tasks
20
Tasks
21
Union
RS
– Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated.
– R and S must be union-compatible.
22
Example - Union
23
Example - Union
List all cities where there is either a branch office
or a property for rent.
city(Branch) city(PropertyForRent)
24
Task: Union Operation
Borrower relation Depositor relation
customer- loan-
customer-
name number account-number
name
Jones L-17
Johnson A-101
Smith L-23
Smith A-215
Hayes L-15
Hayes A-102
Jackson L-14
Turner A-305
Curry L-93
Johnson A-201
Smith L-11
Jones A-217
Williams L-17
Lindsay A-222
Adams L-16
Query 01: find the name of all bank customers who have either an
account or a loan or both. 25
Task: Union Operation
Query 01: find the name of all bank customers who have either an
account or a loan or both.
customer-name
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Adams
26
Set Difference
R–S
– Defines a relation consisting of the tuples that
are in relation R, but not in S.
– R and S must be union-compatible.
27
Example - Set Difference
28
Example - Set Difference
List all cities where there is a branch office but no
properties for rent.
city(Branch) – city(PropertyForRent)
29
Task: Set Difference Operation
Borrower Depositor
customer- loan-
customer-
name number account-number
name
Jones L-17
Johnson A-101
Smith L-23
Smith A-215
Hayes L-15
Hayes A-102
Jackson L-14
Turner A-305
Curry L-93
Johnson A-201
Smith L-11
Jones A-217
Williams L-17
Lindsay A-222
Adams L-16
Query 01: find the names of all customers of the bank who have an
account but not a loan 30
Task: Set Difference Operation
Query 01: find the names of all customers of the bank who have an
account but not a loan
customer-name
Johnson
Turner
Lindsay
31
Intersection
RS
– Defines a relation consisting of the set of all
tuples that are in both R and S.
– R and S must be union-compatible.
32
Example - Intersection
33
Example - Intersection
List all cities where there is both a branch office
and at least one property for rent.
city(Branch) city(PropertyForRent)
34
Task: Intersection Operation
Borrower relation Depositor relation
customer- loan-
customer-
name number account-number
name
Jones L-17
Johnson A-101
Smith L-23
Smith A-215
Hayes L-15
Hayes A-102
Jackson L-14
Turner A-305
Curry L-93
Johnson A-201
Smith L-11
Jones A-217
Williams L-17
Lindsay A-222
Adams L-16
Query 01: find the names of all customers who have both a loan and
and account 35
Task: Intersection Operation
Query 01: find the names of all customers who have both a loan and
and account
customer-name
Hayes
Jones
Smith
36
Student Book
CMS sName prog CR_CMS ISBN title author publisher
1243 Ali BS-SE 1382 111324 Database C Date Addison
1382 Kamran BS-SE 111325 Software Eng Kamran Pearson
2751 Rizwan BE-EE 111326 Software Quality Kamran Orielly
1543 Samir MBBS 111327 Database Connolly Orielly
3265 Imran BE-EE 2751 111328 Software Design Ali Pearson
111329 Data Mining Roger Addison
Book_Issue
CMS ISBN dateIssued dateReturn
1543 111324 20-Jan-14 23-Jan-14
1243 111325 15-Mar-15
2751 111329 23-Dec-13 00-Dec-13
2751 111324 10-Mar-15 11-Mar-15
1243 111329 20-Jan-14 22-Jan-14
1543 111329 13-May-14 20-May-14
37
1243 111325 10-Apr-12 15-Apr-12
Book_Reservation
Student CMS ISBN title author publishe
CMS sName program r
1243 Ali BS-SE 1243 111324 Database C Date Addison
1382 Kamran BS-SE 1243 111325 Software Eng Kamran Pearson
1382 111326 Software Quality Kamran Orielly
Book X Book_Reservation
CMS sName program CMS ISBN title author publisher
1243 Ali BS-SE 1243 111324 Database C Date Addison
1243 Ali BS-SE 1243 111325 Software Eng Kamran Pearson
1243 Ali BS-SE 1382 111326 Software Quality Kamran Orielly
1382 Kamran BS-SE 1243 111324 Database C Date Addison
1382 Kamran BS-SE 1243 111325 Software Eng Kamran Pearson
1382 Kamran BS-SE 1382 111326 Software Quality Kamran Orielly
38
Employee Department
Emp_Id Name JobTitle Gender Dept_Id Dept_Id Dept_name
E101 Sara Software_Eng female D14
D11 Marketing
E102 Alia Finance_Mgr female D12
male D12 Finance
E103 Mustafa Designer D14
male
E104 Javed Salesman D11 D13 HR
E105 Sarmad IT_Manager male D14
D14 IT
39