Relational Algebra
Relational Algebra
(BTIT502-18)
• SQL is a practical
implementation of relational
algebra and calculus
Relational Algebra
• 1970 by EF Codd.
• It gives a step by step process to obtain the result of the query. It uses operators
to perform queries.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Intersection (∩)
5. Set Difference (-)
6. Cartesian product (X)
7. Rename (ρ)
Derived Operations:
1. Natural Join (⋈)
2. Left, Right, Full outer join (⟕, ⟖, ⟗)
3. Division (÷)
1. Select Operator (σ)
• Select Operator is denoted by sigma (σ)
• It is used to find the tuples (or rows) in a relation (or table) which satisfy the
given condition.
• It is similar to “where” clause in Structured Query Language(SQL)
• Syntax of Select Operator (σ) :-
σ Condition/Predicate (Relation/Table name)
OR
σ p(r)
Where: σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula (condition) which
may use connectors like: AND OR and NOT. These relational can use as relational
operators like =, ≠, ≥, <, >, ≤.
Select Operator (σ) Example1
Table: CUSTOMER
---------------
Query:
σ Customer_City="Agra" (CUSTOMER)
Output:
QUERY:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUNT
OR
Table: CUSTOMER
Customer_Id Customer_Name Customer_City
----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:
∏ Customer_Name, Customer_City (CUSTOMER)
Output:
Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Project Operator (∏) Example2
Project Operator (∏) Example 3
NAME STREET CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
Set operations
1. Set union is:
– Suppose A = {i, j, k} and B = {k, x, y}
– Then the union of A and B is {i, j, k, x, y}
– Note that tuples in sets A and B must have the same FORMAT (same number and type of attributes)
to be used in the union operation.
• Suppose there are two relations R and S. The union operation contains
all the tuples that are either in R or S or both in R & S.
• It eliminates the duplicate tuples. The rows (tuples) that are present in
both the tables will only appear once in the union set.
• It is denoted by ∪.
Union Operation
• Syntax of Union Operator (∪) :-
table_name1 ∪ table_name2
OR
R∪S
• A union operation must hold the following condition:
1. R and S must have the attribute of the same number.
2. Duplicate tuples are eliminated automatically.
Union Operator (∪) Example1
Table 1: COURSE
Table 2: STUDENT
Query:
Output:
Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
• Student (roll.no, name, marks, city)
• Department (d.id, dname, s.name)
• Display student name from student and department relations.
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
• Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
• It is denoted by intersection ∩.
• Notation:- R∩S
OR
table_name1 ∩ table_name2
Example1:
• Using the above DEPOSITOR table and BORROW table
• Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
• Output:
CUSTOMER_NAME
Smith
Jones
EXAMPLE 2
Relation 1: COURSE
Course_Id Student_Name Student_Id
--------- ------------ ----------
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Relation 2: STUDENT
Student_Id Student_Name Student_Age
------------ ---------- -----------
S901 Aditya 19
S911 Steve 18
S921 Paul 19
S931 Lucy 17
S941 Carl 16
S951 Rick 18
EXAMPLE 2 (cont.)
• Query:
∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)
• Output:
Student_Name
------------
Aditya
Steve
Paul
Lucy
5. Set Difference:
• Suppose there are two tuples R and S. The set intersection
operation contains all tuples that are in R but not in S.
• Notation: R - S
Relational Algebra 24
Example1: Set Difference
DEPOSITOR RELATION BORROW RELATION
CUSTOMER_NAME ACCOUNT_NO CUSTOMER_NAME LOAN_NO
Jones L-17
Johnson A-101
Smith L-23
Smith A-121
Hayes L-15
Mayes A-321
Jackson L-14
Turner A-176
Curry L-93
Johnson A-273
Smith L-11
Jones A-472
Williams L-17
Lindsay A-284
Query:
∏ CUSTOMER_NAME (DEPOSITOR) - ∏ CUSTOMER_NAME (BORROW)
Relational Algebra 25
• Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
Relational Algebra 26
Example2: Set Difference
• Query
Write relational algebra statement to Select those student names that are present in
STUDENT table but not present in COURSE table.
Relational Algebra 27
• Query
∏ Student_Name (STUDENT) - ∏ Student_Name (COURSE)
• Output:
Student_Name
------------
Carl
Rick
Relational Algebra 28
6. Rename Operation:
• The rename operation is used to rename the output relation.
It is denoted by rho (ρ).
Relational Algebra 29
Example:
• To rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
Relational Algebra 30
Example:
• Table: Customer
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
Relational Algebra 32
7. Cartesian product
• The Cartesian product is used to combine each row in one table with
each row in the other table.
• It is also known as a cross product.
• It is denoted by X.
• A query that lists multiple tables in the FROM clause without a WHERE
clause produces all possible combinations of rows from all tables.
select *
from one, two;
• Notation: R1 X R2
Relational Algebra 33
Cartesian Product
Table One
X A Table Two
1 a X B
4 d 2 x
2 b 3 y
5 v
Relational Algebra 39
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
Relational Algebra 40
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
Relational Algebra 41
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
Relational Algebra 42
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
Relational Algebra 43
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
Relational Algebra 45
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
Relational Algebra 46
...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows X 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y 9 rows
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
Relational Algebra 47
Cartesian Product
•The number of rows in a Cartesian product is the product of the number of rows
in the contributing tables.
• 3x3=9
1,000 x 1,000 = 1,000,000
100,000 x 100,000 = 10,000,000,000
Relational Algebra 48
Quiz
•How many rows (Cardinality) are returned from this query?
select *
from three, four;
Relational Algebra 49
Quiz – Correct Answer
•How many rows are returned from this query?
The query produces 20 rows.
select *
from three, four;
Table Three Table Four Partial Results Set
X A X B X A X B
1 a1 2 x1 1 a1 2 x1
1 a2 2 x2 1 a1 2 x2
2 b1 3 y 1 a1 3 y
2 b2 5 v 1 a1 5 v
4 d 1 a2 2 x1
1 a2 2 x2
5*4=20 1 a2 3 y
1 a2 5 v
2 b1 2 x1
Relational Algebra 2 b1 2 x2 50
Example1: Cartesian product
Relational Algebra 51
Input:
EMPLOYEE X DEPARTMENT
Output:
Relational Algebra 52
Example2:
• Table 1: R Table 2: S
Col_A Col_B Col_X Col_Y
AA 100 XX 99
BB 200 YY 11
CC 300 ZZ 101
• Query:
Lets find the cartesian product of table R and S.
• RXS
Relational Algebra 53
Output:
Col_A Col_B Col_X Col_Y
----- ------ ------ ------
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101
Relational Algebra 54
• Thank You
• Any Queries?????