0% found this document useful (0 votes)
16 views

Relational AlgebraNewUp

Relational algebra is a procedural query language that takes relations as input and produces relations as output using various operators. The fundamental operations are selection, projection, union, intersection, and rename. Examples demonstrate how to write queries using these operations.

Uploaded by

Hasitha Sanjaya
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)
16 views

Relational AlgebraNewUp

Relational algebra is a procedural query language that takes relations as input and produces relations as output using various operators. The fundamental operations are selection, projection, union, intersection, and rename. Examples demonstrate how to write queries using these operations.

Uploaded by

Hasitha Sanjaya
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/ 21

Relational Algebra

M.Y Abdur Rahman


 It is a procedural query language [which takes instances of
relations as input and yields instances of relations as output].
 It uses operators to perform queries.
 Operator can be either,
Relational  Unary
 Binary
algebra  Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
 The fundamental operations of relational algebra are as follows −
• Selection

Relational •

Projection
Union
algebra cont.., •

Intersection
Rename
 It selects tuples that satisfy the given predicate from a relation.
[it is used to select or retrieve data from a relation that satisfies a
particular selection condition.]

Selection
Selection operator
Relation

operation 𝜎 ¿ 𝑠𝑒𝑙𝑒𝑐𝑡𝑖𝑜𝑛 >( 𝑅 )


𝑐𝑜𝑛𝑑𝑜𝑡𝑖𝑜𝑛

a particular data
retrieved from a
relation
(=,>,<,=>,=<)
 Question 01: select the Department tuples whose department id is
d03.

𝜎 𝑑𝑒𝑝𝑡 𝑖𝑑

¿ 𝑑 03 ′
( 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 )

Question  Question 02: select the Department tuples whose department


number is d03 and have the department income amount greater
than of 30000.

𝜎 𝑑𝑒𝑝𝑡 ¿ ′ 𝑑 03 ′ 𝑎𝑛𝑑 𝑑 𝑖𝑛𝑐𝑜𝑚𝑒> 30000 ( 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 )


𝑖𝑑
 Question 02: select the Department tuples whose department
number is d03 and with department income amount greater than
of 30000 or d05 and with department income amount greater than
of 25000

𝜎 ( 𝑑𝑒𝑝𝑡
Question ❑( 𝑑𝑒𝑝𝑡
𝑖𝑑


¿′ 𝑑 03′ 𝑎𝑛𝑑 𝑑𝑖𝑛𝑐𝑜𝑚𝑒 > 30000 ) 𝑜𝑟

𝑖𝑑 ¿ 𝑑 05 𝑎𝑛𝑑 𝑑𝑖𝑛𝑐𝑜𝑚𝑒 >25000 ) ( 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 )
 It projects column(s) that satisfy a given predicate.
 Duplicate rows are automatically eliminated, as relation is a set

projection
Projection operator
Relation

operation 𝜋 ¿ 𝑎𝑡𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒 𝑙𝑖𝑠𝑡 > ¿( 𝑅) ¿

a particular data
retrieved from a
relation
(=,>,<,=>,=<)
 Question 01: List the following from the department table.
Department id, name, income

𝜋 𝑑𝑒𝑝𝑡 𝑖𝑑
, 𝑑𝑒𝑝𝑡 𝑛𝑎𝑚𝑒 , 𝑑𝑒𝑝𝑡 𝑖𝑛𝑐𝑜𝑚𝑒 ( 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 )

Question  Question 02: list in the Department tuples whose department


number is d03 and have the department income amount greater
than of 30000.

𝜋 𝑑𝑒𝑝𝑡 ( 𝜎 ¿ ¿ 𝑑𝑒𝑝𝑡 𝑖𝑑 ¿′ 𝑑 03 ′ 𝑎𝑛𝑑 𝑑𝑖𝑛𝑐𝑜𝑚𝑒 >30000 ) ( 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 ) ¿


𝑖𝑑
 The results of relational algebra are also relations but without any
name. The rename operation allows us to rename the output
relation

rename
Rename operator
Old Relation name

operation
𝜌 𝑠( 𝑎 1 , 𝑎 2 , 𝑎𝑛) ( 𝑅 )

New relation name attributes


 Question : rename student table as softstudent.

𝜌 𝑠𝑜𝑓𝑡 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 )

Question  Question : rename name in student table as st_fnama.

𝜌 𝑛𝑎𝑚𝑒 / 𝑠𝑡 𝑓𝑛𝑎𝑚𝑒
( 𝑠𝑜𝑓𝑡𝑠𝑡𝑢𝑑𝑒𝑛𝑡 )
 Question : select the student first name and last name of all the
student who are studying in the IT division.

𝜋 𝑠𝑡 𝑓𝑛𝑎𝑚𝑒 , 𝑠 𝑡 ¿ 𝑙𝑛𝑎𝑚𝑒 ( 𝜎 𝑑𝑒𝑝𝑡 ′ ′ ( 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ) )


𝑛𝑎𝑚𝑒 ¿ 𝐼 𝑇

Question  Question : use the rename operator to store the and answer in a
temp location called stsoft. As a relation.

𝜌 𝑠𝑡𝑠𝑜𝑓𝑡
[𝜎 𝑑𝑒𝑝𝑡𝑛𝑎𝑚𝑒 ¿ 𝐼 𝑇
′ ′ ( 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ) ]
 It performs binary union between two given relations.
 Where r and s are either database relations or relation result set
(temporary relation).
 For a union operation to be valid, the following conditions must
union hold −
• r, and s must have the same number of attributes.
operation • Attribute domains must be compatible.
• Duplicate tuples are automatically eliminated.

r U s = { t | t ∈ r or t ∈ s}
 Question :Predict the names of the students who have either
passed in both IT or in history or both.

𝜋 𝐼𝑇 ( 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ) ∪ 𝜋 𝐻𝑖𝑠𝑡𝑜𝑟𝑦 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 )

questions  Question :Retrieve branch and section of student from table


student1 and student2.

𝜋 𝑏𝑟𝑎𝑛𝑐h , 𝑠𝑒𝑐𝑡𝑖𝑜𝑛 ( 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 1 ) ∪ 𝜋 𝑏𝑟𝑎𝑛𝑐h ,𝑠𝑒𝑐𝑡𝑖𝑜𝑛 ( 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 2)


 It displays the common values in R1 & R2. It is denoted by ∩.
 In relational algebra if R1 and R2 are two instances of relation
then,
 R1 ∩ R2 ={ x | x€ R1 and x € R2}
 That is, the intersection of R1 and R2 only those tuples will be
intersection present that are in both R1 and R2

operation
𝜋 𝑟𝑒𝑔𝑛𝑜 ( 𝑅 1 ) ∩ 𝜋 𝑟𝑒𝑔𝑛𝑜 ( 𝑅 2 )
 Question : Find all the customers whose account is in the bank and
have taken out a loan.

question

𝜋 𝑛𝑎𝑚𝑒 ( 𝐷𝑒𝑝𝑜𝑠𝑖𝑡𝑜𝑟 ) ∩ 𝜋 𝑛𝑎𝑚𝑒 ( 𝐵𝑜𝑟𝑟𝑜𝑤𝑒𝑟 )


 Question 01: write a SQL function to accept 2 integer values and
to return sum of the value?
create function sum(@a int, @b int)
returns int
as
begin
SQL function return @a+@b
end;

select dbo.sum(6,4);
 Question 02: write a function to update the status of the
employee table by multiplying a value passed as a parameter?

create function statusupdte(@status int, @val int)


returns int
as
begin
return @status* @val
end;

select dbo.statusupdte(status,4)
from employee;
 Question 01: Write a stored procedure to select all from the
employee table.

SQL stored
procedure
 Question 02: Write a stored procedure to select all details of the
employee who is having the salary amount 15000 employee table.

You might also like