Relational Algebra
Relational Algebra
ALGEBRA
Content
2
s
What is Relational Algebra?
Operations in Relational Algebra
Extended Relational Algebra
Operations
Assignments
Relational model Relational algebra/Relational calculas
RDBMSSQL
What is Relational
3
Algebra?
It is a procedural query language.
Consists of set of operations.
Takes one (unary) or two (binary) relations as input
& produce a new relation as output.
Introduced by E. F. Codd in 1970 as a basis for a
database query languages.
What is a Query
4
Language?
Database language.
Use for retrieving informations from database.
Two types:-
- Procedural
Specifies what data are needed & how to
get those data.
- Non-procedural
Specifies what data are needed except how
to get those data.
Operation
5
s Select
Project
Union FUNDAMENT
Set Difference AL
Cartesian OPERATIONS
Product
Rename
Set Intersection
Natural Join
Semi Join OTHER
OPERATIO
Theta Join
NS
Assignment
Outer Join
Division
Select Operation
6
(σ)
Unary operation.
Selects tuples (rows) that satisfy the given predicate
(condition) from a relation (table).
σp(r)
PREDICATE RELATION
Harry 34 80 Harry 34 80
Helena 54 54
Sally 28 64
Peter 34 80
George 29 70
Helena 54 54
б age>34 ^ weight=54(person)
Peter 34 80
name age weight
Helena 54 54
Project Operation
9
(Π)
Unary operation.
Selects specified attributes (columns) from
a relation.
ΠA1, A2, An(r)
RELATION
ATTRIBUTES
Exampl
10
e
employee(name, age,
salary)
1. Find the name of all employees.
Ans: Πnameemploye
(employee) ∏ (employee)
name
Πname(σsemester=“2nd"(course))
Illustratio
14
n
cours
∏ name(бsemester=“1st"(course))
name e
semester teacher name
C1 1st T1 C1
C2 2nd T2
∏ name (course
C3 3rd T3 (б semester=“2nd" ))
name
C4 4th T4 C2
C1 2nd T1 C1
∏ name(бsemester=“1st"(course))U ∏name(бsemester=“2nd"(course))
name
C1
C2
Set Difference
15
(-)
Binary operation.
Returns the tuples which are present in first relation
(r) but are not in the second relation (s).
r −s
Like union the conditions are same for a valid r –
s
operation.
Exampl
16
e
course(name, semester, teacher)
1. Find the name of all courses taught in the 1st
semester but not in 2nd semester.
Ans: Πname(σsemester=“1st"(course))
-
Πname(σsemester=“2nd"(course))
Illustratio
17
n
cours ∏ name(бsemester=“1st"(course))
name e
semester teacher name
C1
C1 1st T1
C3
C2 2nd T2
∏ name (course
C3 1st T3 (б semester=“2nd" ))
name
C4 4th T4 C2
C1 2nd T1 C1
∏ name(бsemester=“1st"(course))
- ∏name(бsemester=“2nd"(course))
name
C3
Cartesian Product
18
(x)
Binary operation.
Combines information from any two relations (r &
s). r x s
It defines a relation by concatenating every tuple
of relation r with every tuple of relation s.
Illustratio
19
n
perso city
name n age weight city
Πname(σsemester=“2nd"(course))
Illustratio
24
n
cours ∏ name(бsemester=“1st"(course))
name e
semester teacher name
C1
C1 1st T1
C3
C2 2nd T2
∏ name (course
C3 1st T3 (б semester=“2nd" ))
name
C4 4th T4 C2
C1 2nd T1 C1
∏ name(бsemester=“1st"(course)) ∏name(бsemester=“2nd"(course))
name
C1
Natural Join
25
(⋈)
Binary operation.
r⋈s
where,
- r & s are relations.
The result is the set of all combinations of tuples in r
and s that are equal on their common attribute
names.
Natural Join only if there is at least one
common attribute that exists between two
relations
Illustratio
26
n
t1 t2
a b b c
a1 b1 b1 c1
a2 b2 b2 c2
a3 b3 b4 c4
t1 ⋈
a t2 b c
a1 b1 c1
a2 b2 c2
Semi Join
27
(⋉)
Similar to the Natural Join.
r⋉s
where,
- r & s are relations.
Outputs only those set of tuples in r for which
there is a tuple in s that is equal on their common
attribute names.
Illustratio
28
n
t1 t2
a b b c
a1 b1 b1 c1
a2 b2 b2 c2
a3 b3 b4 c4
t1 ⋉
a t2 b
a1 b1
a2 b2
Theta Join
29
(θ)
Variant of the Natural Join.
Combine a selection & a Cartesian product into
a single operation.
r ⋈θs = σθ(r x s)
where,
- r & s are relations.
- θ is a predicate.
Assignment
30
Operation
Writing a relational algebra expression
by assigning parts of it to temporary
relation variables.
Works like assignment in a programming
language.
Denoted by .
Illustratio
31
n
cours r1 ∏ name(бsemester=“1st"(course))
name e
semester teacher name
C1
C1 1st T1
C3
C2 2nd T2
r2 ∏ name (course
C3 1st T3 (б semester=“2nd" ))
name
C4 4th T4 C2
C1 2nd T1 C1
r1
r2
name
C1
Outer
32
Join
Deals with missing information.
Works in a similar fashion like natural join, but
preserves those tuples that would be lost in the join
by creating tuples in the result containing null
values.
Three types:
- Left Outer Join
- Right Outer Join
- Full Outer Join
Left Outer Join
33
(=⋈)
Takes all the tuples in the left relation that did not
match with any tuple in the right relation.
Pads the tuples with null values for all other
attributes from the right relation.
Adds them to the result of the natural join.
r =⋈ s
where,
- r & s are relations.
Illustratio
34
n
t1 t2
a b b c
a1 b1 b1 c1
a2 b2 b2 c2
a3 b3 b4 c4
t1 =⋈
a t2 b c
a1 b1 c1
a2 b2 c2
a3 b3 null
Right Outer Join
35
(⋈=)
Takes all the tuples in the right relation that did not
match with any tuple in the left relation.
Pads the tuples with null values for all other
attributes from the left relation.
Adds them to the result of the natural join.
r ⋈= s
where,
- r & s are relations.
Illustratio
36
n
t1 t2
a b b c
a1 b1 b1 c1
a2 b2 b2 c2
a3 b3 b4 c4
t1 ⋈=
a t2 b c
a1 b1 c1
a2 b2 c2
null b4 c4
Full Outer Join
37
(=⋈=)
Performs both left & right outer join operations.
Pads the tuples with null values from the right
a1 b1 b1 c1
a2 b2 b2 c2
a3 b3 b4 c4
t1 =⋈=
a t2 b c
a1 b1 c1
a2 b2 c2
a3 b3 null
null b4 c4
Division
39
Operation
Binary operation.
r/s
where,
- r is the dividend relation & s is the divisor relation.
- All the attributes in s also appear in r.
- s is not empty.
The result consists of the restrictions of tuples in r to the
attribute names unique to r, i.e., in the header of r
but not in the header of s, for which it holds that all
their combinations with tuples in s are present in r.
Illustratio
40
n
complete DBProjec
d
student task t task
Fred Database1 Database1
Fred Database2 Database2
Fred Compiler1
Eugene Database1
Sara Database1 completed /
Sara Database2 DBProjectstudent
Eugene Compiler1 Fred
Sara
If DBProject contains all the tasks of the Database project, then the result of the division
above contains exactly the students who have completed both of the tasks in the Database
project.
Extended Relational Algebra
41
Operations
• Generalized
Projection
• Aggregation
Generalized
42
Projection
Extension of projection operation.
Allows arithmetic operations & string functions to be
used in the projection list.
Exampl
43
e
employee(name, age, salary)
1. Find the name & monthly salary of all
employees. Ans: Πname, salary/12(employee)
Aggregation
44
(G)
Permits the use of aggregate functions (sum,
average, min, max, count)
- take a collection of values & return a single value as a
result.
G1,…,GnGF1(A1),…,Fm(Am)(E)
where,
- E => Any relational algebra expression
- G1,…,Gn => List of attributes on which to group
- F1,…,Fm => Aggregate functions
- A1,…,Am => Attribute names
Example
45
s
instructor (id, name, dept_name, salary)
teaches (id, course_id, sec_id, semester,
year)
1. Find out the sum of salaries of all instructors
Gsum(salary)(instructor)
2. Find the total no. of instructors who teach a course in the
Spring 2010 semester
Gcount-distinct(id)(σsemester=“Spring” ^ year=2010(teaches))
3. Find out the average salary of all instructors
Gaverage(salary)(instructor)
4. Find out the average salary in each department
dept_nameGaverage(salary)(instructor)
Assignment
46
s
1. instructor (id, name, dept_name, salary)
teaches (id, course_id, sec_id, semester,
year)
Find the names of all instructors in the Physics
department together with the course-id of all
courses they taught.
Assignments
47
(contd…)
2. employee (emp_name, street, city)
works (emp_name, comp_name,
sal) company (comp_name, city)
manages (emp_name,
a) manager_name)
Find the names of all employees who work for 1st Bank Co-orporation.
b) Find the names & cities of residence of all employees who work for
1st Bank Co-orporation.
c) Find the names, street addresses & cities of all employees who work
for
1st Bank Co-orporation & earn more than 10,000.
d) Find all employees in the DB who live in the same cities as the
companies for which they work.
e) Find all employees in the DB who live in the same city & the same
f) street as do their managers.
Find all employees in the DB who don’t work for 1st Bank Co-
orporation.
Assignments
48
(contd…)
3. sailors (sid, sname, rating,
age) reserved (sid, bid, day)
boats (bid, bname, color)
a) Find the names of the sailors who have reserved a red
or green boat.
b) Find the names of the sailors with age over 20 who
have not reserved a red boat.
c) Find the names of the sailor who have reserved all
boats.
d) Find the color of the boat reserved by Smith.
Assignments
49
(contd…)
4. staff (sno, sname, pos, dob, sal,
bno) branch (bno, street, city,
pincode)
propertyforrent (pno, street, city,
pincode, type,
rooms, rent, ownerno,
sno, bno)
a) List the names of all managers who earn more than
25000.
b) List the staff names who manage properties for rent in
Kolkata.
c) List the names of staff who currently don’t manage
Reference
50
s
Database System Concepts, 6th edition,
Silberschatz, Korth, Sudarshan
www.tutorialspoint.com/dbms/relational_algebra.ht
m
www.cs.sjsu.edu/faculty/lee/cs157/26FRelational_
Algebra(Gupta).ppt
51