0% found this document useful (0 votes)
10 views54 pages

Relational Algebra

Uploaded by

Ankit Dahiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views54 pages

Relational Algebra

Uploaded by

Ankit Dahiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Database Management System

(BTIT502-18)

Module 2: Relational query languages

Relational Algebra, Tuple and domain relational


calculus
1
Query Language
• In simple words, a Language
which is used to store and
retrieve data from database
is known as query language.
For example – SQL

• SQL is a practical
implementation of relational
algebra and calculus
Relational Algebra
• 1970 by EF Codd.

• Relational algebra is a Procedural Query Language that works on relational model.

• Procedural query language tells what data to be retrieved and how to be


retrieved.

• It gives a step by step process to obtain the result of the query. It uses operators
to perform queries.

• The purpose of a query language is to retrieve data from database or perform


various operations such as insert, update, delete on the data.
Types of operations in relational algebra
• 1. Basic Operations
• 2. Derived Operations

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
---------------

Customer_Id Customer_Name Customer_City


----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi

Query:
σ Customer_City="Agra" (CUSTOMER)

Output:

Customer_Id Customer_Name Customer_City


----------- ------------- -------------
C10100 Steve Agra
C10111 Raghu Agra
Select Operator (σ) Example 2
Select Operator (σ) Example 3
LOAN Relation
BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

QUERY:
σ BRANCH_NAME="perryride" (LOAN)
Output:
BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300


2. Project Operation (∏) :
• It is used to select desired columns (or attributes) from a table (or relation). Rest of
the attributes are eliminated from the table.
• It is denoted by ∏.
• Project operator in relational algebra is similar to the Select statement in SQL.
• Syntax (Notation) of Project Operator (∏) :-
∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.

OR

∏ column_name1, column_name2, ...., column_nameN (table_name)


Project Operator (∏) Example1
• A table CUSTOMER with three columns, we want to fetch only two columns of the table, which we can do with the help of
Project Operator ∏.

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 Main Harrison


Smith North Rye
Hays Main Harrison
Curry North Rye
Johnson Alma Brooklyn
Brooks Senator Brooklyn
Query:
∏ NAME, CITY (CUSTOMER)
Output: NAME 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.

2. Set intersection is:


– Suppose A = {i, j, k} and B = {k, x, y}
– Then the intersection of A and B is {k}
– Note that tuples in sets A and B must have the same FORMAT (same number and type of attributes)
to be used in the intersection operation.

3. Set difference is:


– Suppose A = {i, j, k} and B = {k, x, y}
– Then the difference of A and B is {i, j}
– Note that tuples in sets A and B must have the same FORMAT (same number and type of attributes)
3. Union Operation:
• It is used to select all the rows (tuples) from two tables (relations).

• 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

Course_Id Student_Name Student_Id


--------- ------------ ----------
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931

Table 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

Query:

∏ Student_Name (COURSE) ∪ ∏ Student_Name (STUDENT)

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.

• ∏ name (Student) U ∏ s.name(Department)


Union Operator (∪) Example2

DEPOSITOR RELATION BORROW RELATION

CUSTOMER_NAME ACCOUNT_NO CUSTOMER_NAME LOAN_NO

Johnson A-101 Jones L-17

Smith A-121 Smith L-23

Mayes A-321 Hayes L-15

Turner A-176 Jackson L-14

Johnson A-273 Curry L-93

Jones A-472 Smith L-11

Lindsay A-284 Williams L-17


Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

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 used to select common rows (tuples) from two tables (relations).

• 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.

• It is denoted by intersection minus (-).

• 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 (ρ).

• Rename (ρ) Syntax:


ρ(new_relation_name, old_relation_name)

Relational Algebra 29
Example:
• To rename STUDENT relation to STUDENT1.

ρ(STUDENT1, STUDENT)

Relational Algebra 30
Example:
• Table: Customer

• Query: fetch customer names and rename the resulted


relation to CUST_NAMES.
Relational Algebra 31
• Query:

ρ(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 s105d01


34
...
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

Relational Algebra s105d01


35
...
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

Relational Algebra s105d01


36
...
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

Relational Algebra s105d01


37
...
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

Relational Algebra s105d01


38
...
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

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

A Cartesian product is rarely the desired result of a query.

Relational Algebra 48
Quiz
•How many rows (Cardinality) are returned from this query?

Table Three Table Four


X A
X B
1 a1
2 x1
1 a2
2 x2
2 b1
3 y
2 b2
5 v
4 d

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

Table 1: EMPLOYEE Table2: DEPARTMENT

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME


1 Smith A A Marketing
2 Harry C B Sales
3 John B C Legal

• Input: EMPLOYEE X DEPARTMENT

Relational Algebra 51
Input:
EMPLOYEE X DEPARTMENT
Output:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME


1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal

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?????

You might also like