Unit III Relational Model 2
Unit III Relational Model 2
Part-2
Operations
Unary Binary
Operate on one Operate on pairs of
relation relations
Set Cartesian
Select Project Rename Union
difference product
Relational Algebra
Additional operations
Set
intersection
Division
Relational Algebra
Select operation
• Need
➢ Selects tuples that satisfy a given selection condition
• Basics
➢ Denoted by sigma (σ)
➢ σ<selection condition>(R)
➢ Visualized as horizontal partition of relation into two sets of tuples
➢ Allows comparisons =, , >, , <, in the selection predicate
➢ Boolean expression specified <selection condition>, made up of a
number of clauses
<attribute name><comparison op><constant value>
Or
<attribute name><comparison op><attribute name>
<attribute name>: Name of attribute of R
<comparison op>: One of operator =, , >, , <,
<constant value>: Constant value from attribute domain
Relational Algebra
Select operation
• Relation r
A B C D
1 8
5 8
13 4
21 11
A B C D
1 8
21 11
Relational Algebra
Select operation (contd.)
The loan relation
• Example:
➢Input: branch-name=“Perryridge”(loan)
➢Output:
• salary>90000 (instructor)
Relational Algebra
Select operation (contd.)
SELECT in SQL
• Need
➢Selects certain columns from the table and discards other
columns
• Basics
➢Denoted by pi (Π)
➢Visualized as vertical partition of relation into two
relations (required and discarded)
➢Duplicate elimination: Removes any duplicate tuples
• Notation: Π<attribute list>(R)
Relational Algebra
Project operation (contd.)
• Relation r
A B C
11 21
22 21
33 21
44 51
• A,C (r)
A C
A C
21
21
21 =
21
21
51
51
Relational Algebra
Project operation (contd.)
• Example
Πloan-number, amount(loan)
Output:
PROJECT in SQL
• Example
➢ To find all customers with either an account or a loan
• Relations r, s R S
A B A B
1 2
2 5
1
RS
A B
1
2
1
5
Relational Algebra The section relation
• Need
➢ To find tuples that are in one relation (R) but are not in another (S)
• Basics
➢ Denoted by ‘-’
➢ Notation: R – S
➢ Not commutative
R-S S-R
➢ Set differences must be taken between compatible relations
▪ r and s must have the same arity
▪ attribute domains of r and s must be compatible
Relational Algebra
Set difference operation (contd.)
• Relations R, S R
S
A B
A B
1
2
2
5
1
R-S
A B
1
1
Relational Algebra
Set difference operation (contd.)
• Find all the courses taught in the Fall 2009 semester but
not in Spring 2010 semester
course_id ( semester = “Fall” ∧ year=2009 (section)) − course_id (
semester = “Spring” ∧ year=2010 (section))
The section relation
• Output:
A B C D E
1 10 a
2 11 b
21 c
A B C D E
1 10 a
• R×S
1 11 b
1 21 c
2 10 a
2 11 b
2 21 c
Relational Algebra
Cartesian product operation (contd.)
Example
• Find the names of all instructors in the Physics department together
with course_id of all courses
dept_name = “Physics” (instructor × teaches)
• ρx(E),
• Expression returns the result of expression E under the name x
• ρx (A1 x A2 x … x An)(E)
• Returns the result of expression E under the name x and with
attributes renamed to A1,A2,…,An
Relational Algebra
Rename operation (contd.)
Example Query: Find the highest salary in university