0% found this document useful (0 votes)
18 views70 pages

Relational Algebra Final PPT 08-06-2023

Uploaded by

Putta Swamy
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)
18 views70 pages

Relational Algebra Final PPT 08-06-2023

Uploaded by

Putta Swamy
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/ 70

Introduction to Relational Algebra

Puttaswamy B S
Assistant Professor
Learning Outcomes

• Unary and Binary relational operations


• Examples of simple queries in relational algebra
Introduction to Query Language
• To retrieve information from a database.

1. Procedural Query language


2. Non-procedural Query language
• what data to be retrieved from

database and how to retrieve it.


• what data to be retrieved from

database but doesn’t tell how to


retrieve it.
Introduction to Relation Algebra
• Algebra – A formal structure that contains sets and operations,
with operations being performed on those sets.
• A procedural query language which is the core of any relational
query languages available for the database.
• A framework for query implementation and optimization.
• A procedural query language. It gives a step by step process to
obtain the result of the query.
Various Relational Algebra operations
Select operation
• A unary operation, performs the operation of selecting particular
tuple or a row which satisfies a specific predicate from a
relation.

• σ<Selection condition>(R)
• σ - represent the SELECT command
• <selection condition> - represent the condition for selection.
• (R) - represent the Relation or table from which we are making a
selection of the tuples.
σ Name and Age>21 (Student_Details)
• σname = “Nicholas_sparks”(Novels)

• σname = “Nicholas_sparks” and price = “300”(Novels)


Details of the members who were born on 21/10/1997.
• The SELECT operation is commutative; that is,

• σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R))

• σ<cond1>(σ<cond2>(...(σ<condn>(R)) ...)) = σ<cond1>

AND<cond2> AND...AND <condn>(R)


Project Operation

• A unary operation, projects column(s) which satisfy a particular


predicate (given predicate).

• ΠA1, A2, An (r)

• A1, A 2, An are the attributes of relation r.

• As the relation is set, duplicate rows are automatically


eliminated.
• Πname, author (Novels)
• Retrieve details of all those employee whose age is
greater than 26
RENAME Operation

To retrieve the first name, last name, and


salary of all employees who work in
department number 5
Rename the Member relation as LibraryMemebr
Syntax : ρ<New Name>(New Relation)

ρ STD_ID, STD_NAME, STD_ADDRESS(STUDENT)


Relational Algebra Operations from Set Theory
UNION

• Fetches the data from both tables and projects it accordingly.


• It is denoted through “Union Symbol(U)”

•Both the relations compulsory to have same number of attributes.


•Both the relations compulsory to have same domain for attributes.

Syntax : X1 U X2 , where X1 & X2 are two different relations


satisfying the above two conditions.
Set Difference Operations

• Fetch the data which is not present in second relation.


• Denoted by “Minus(-)”.
Syntax : X1 - X2 or X2 - X1 , where X1 & X2 are two different relations having some attributes.
Intersection
• Retrieves all rows common to both relations.
• The ∩ symbol denotes the intersection operation.
Cartesian Product (X)
Join Operation

• Combines related tuples from different relations, if and only if a

given join condition is satisfied.

• The Cartesian product of two tables followed by the selection

operation.

• Join operation is denoted by ⋈ ⟗

• R ⟗ <join condition> S
Inner Join

• Return all the tuples that have matching values in both the
tables.
• All those tuples which do not satisfy the required conditions are
excluded.
Natural Join

• Returns the values of the two tables on the basis of a common


attribute that has the same name and domain.
• Does not use any comparison operator.
• Removes the duplicate attribute from the results.
• It is denoted by ⋈
• A*⟗ <join condition> B,
• OR
• A*⟗ (<join attributes 1>), (<join attributes 2>) B
Theta Join

• Combines the tuples from different relations according to the


given theta condition.
• Denoted by theta () symbol.

• Uses all kind of comparison operator {=, <, ≤, >, ≥, ≠}.


Equi Join

• Same as theta join.


• Uses the equivalence condition (equality operator) for fetching
the values of two tables.
To retrieve the name of the manager of each
department
Outer Join

• Return the records in both the tables depending on the condition.


• If some attribute value is not present for any one of the tables
it returns NULL in the respective row of the table attribute.
Left Outer Join(⟕)

• Returns all the tuples of the left relation and the tuples of the
right relation that has matching values in the left relation.
• If there is no matching result in the right table, it will return
NULL values in that field.
• SELECT * FROM TableA
• LEFT OUTER JOIN TableB
• ON tableA.name = tableB.name;
Right Outer Join

• Returns all the attributes of right hand relation and matching


attributes in left hand relation is found and displayed.
• If no matching is found, then NULL is displayed.
• SELECT * FROM tableA
• RIGHT OUTER JOIN tableB
• ON tableA.name = tableB.name
Full Outer Join

• Combines all the tuples from the left relation and right relation
which are having the same value on the common attribute (it's a
union of both).
• Also, they will have all the remaining tuples which are not common
on in both the relations.
• SELECT * FROM TableA
• FULL OUTER JOIN TableB
• ON TableA.name = TableB.name;
DIVISION OPERATION
Introduction
• Dividing one number by another number.
• Division operation is a binary relational operation that returns
selected attributes in first relation, matching with every tuple in
second relation.
• Syntax: X÷Y can apply if and only if:
• Attributes of Y is proper subset of Attributes of X
• Proper Subset:
• A= {1,2,3} is a proper subset of B = {1,2,3,4} because the element 4
is not in the first set A.}
• After division operation, remaining attributes in table will be = (All
attributes of X – All Attributes of Y).
• After division operation, Relation will return those tuples from
relation X which are associated to every tuple of Y.
Why Division?

• When we have to evaluate queries which contain the keyword ‘all

Some instances where division operator is used are:


• Which person has account in all the banks of a particular city?
• Which students have taken all the courses required to graduate?
Example of Simple Queries in
Relational Algebra
Consider the Shipping Database contains
following tables:
• Suppliers(sID, sName, address)
• Parts(pID, pName, colour)
• Catalog(sID, pID, price)
Write a Relational Algebra Queries of the
following
• Find the names of all red parts
• Find all prices for parts that are red or green. (A part may have
different prices from different manufacturers.)
• Find the sIDs of all suppliers who supply a part that is red or
green.
• Find the sIDs of all suppliers who supply a part that is red and
green.
• Find the names of all suppliers who supply a part that is red or
green.
PREVIOUS YEAR QUESTION PAPER
DISCUSSION
Answer

You might also like