0% found this document useful (0 votes)
42 views67 pages

Relational Algebra

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

Relational Algebra

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

Dr. Kuldeep N.

Tripathi
Assistant Professor
CSE
Database Management System (DBMS)
Relational Algebra operations
Relational Algebra

• Relational Algebra is a procedural query language, which takes Relation as input and generates
relation as output. Relational algebra mainly provides a theoretical foundation for relational
databases and SQL.

• The relational algebra consists of a set of operations that take one or two relations as input and
produce a new relation as their result.

• Some of these operations, such as the select, project, and rename operations, are called unary
operations because they operate on one relation. The other operations, such as union, Cartesian
product, and set difference, operate on pairs of relations and are, therefore, called binary operations.
Concept of Relational Algebra
Relational Algebra

▪ A procedural language consisting of a set of operations that take one or two relations as input and produce
a new relation as their result.
▪ Six basic operators
• select: 
• project: 
• union: 
• set difference: –
• Cartesian product: x
• rename: 
Relational Algebra
operations:

• Selection (σ): Selection operation is used to filter the records from the relation.
• Projection (π): Projection operation is used to filter the columns from the given relation.
• Cross Product(X): This operation is used
• Union (U):
Union operation in relational algebra is the same as union operation in set theory, the only constraint
is for the union of two relations both relations must have the same set of Attributes.
• Set Difference (-):
Set Difference in relational algebra is the same set difference operation as in set theory with the
constraint that both relations should have the same set of attributes.
• Rename (ρ):
Rename is a unary operation used for renaming attributes of a relation. ρ (a/b)R will rename the
attribute ‘b’ of the relation by ‘a’.
EXAMPLE OF A INSTRUCTOR
RELATION
Select Operation

▪ The select operation selects tuples that satisfy a given predicate. The select operation selects tuples that
satisfy a given predicate.
▪ Notation:  p (r): We use the lowercase Greek letter sigma (σ) to denote selection
▪ p is called the selection predicate: The argument relation is in parentheses after the σ. The predicate appears
as a subscript to σ
• Thus, to select those tuples of the instructor relation where the instructor is in the “Physics” department, we write:

σdept name =“Physics” (instructor)


▪ Example: select those tuples of the instructor relation where the instructor is in the “Physics” department.
• Query
 dept_name=“Physics” (instructor)
• Result
THE SELECT OPERATOR
SELECTION CONDITION
Select Operation (Cont.)

▪ We allow comparisons using


=, , >, . <. 
in the selection predicate.
▪ We can combine several predicates into a larger predicate by using the connectives:
 (and),  (or),  (not)
▪ Example: Find the instructors in Physics with a salary greater $90,000, we write:
 dept_name=“Physics”  salary > 90,000 (instructor)
▪ The select predicate may include comparisons between two attributes.
• Example, find all departments whose name is the same as their building name:
•  dept_name=building (department)
Select Operation (Cont.)
Select Operation Example
EXAMPLES OF SELECT EXPRESSIONS
Project Operation

▪ A unary operation that returns its argument relation, with certain attributes left out.
▪ Notation:
 A1,A2,A3 ….Ak (r)
where A1, A2, …, Ak are attribute names and r is a relation name.
▪ The result is defined as the relation of k columns obtained by erasing the columns that are not listed
▪ Duplicate rows removed from result, since relations are sets
THE PROJECT OPERATOR
Project Operation
EXAMPLES OF PROJECT EXPRESSIONS
The Project Operation
• Suppose we want to list all instructors’ ID, name, and salary, but we do not care about the dept name.

• The project operation allows us to produce this relation. The project operation is a unary operation that
returns its argument relation, with certain attributes left out.

• Since a relation is a set, any duplicate rows are eliminated. Projection is denoted by the uppercase Greek
letter pi (Π). We list those attributes that we wish to appear in the result as a subscript to Π. The argument
relation follows in parentheses. We write the query to produce such a list as:

ΠID, name, salary(instructor)


Project Operation Example

▪ Example: eliminate the dept_name attribute of instructor


▪ Query:
ID, name, salary (instructor)
▪ Result:
Project Operation Example
The Rename Operation

▪ The results of relational-algebra expressions do not have a name that we can use to refer to them. The
rename operator,  , is provided for that purpose
▪ The expression:
x (E)
returns the result of expression E under the name x
▪ Another form of the rename operation:
x(A1,A2, .. An) (E)
R E N A M E O P E R AT I O N
The Assignment Operation

▪ It is convenient at times to write a relational-algebra expression by assigning parts of it to temporary relation


variables.
▪ The assignment operation is denoted by  and works like assignment in a programming language.
▪ Example: Find all instructor in the “Physics” and Music department.

Physics   dept_name=“Physics” (instructor)


Music   dept_name=“Music” (instructor)
Physics  Music

▪ With the assignment operation, a query can be written as a sequential program consisting of a series of
assignments followed by an expression whose value is displayed as the result of the query.
Cartesian-Product Operation

▪ The Cartesian-product operation (denoted by X) allows us to combine information from any two relations.
▪ Example: the Cartesian product of the relations instructor and teaches is written as:
instructor X teaches
▪ We construct a tuple of the result out of each possible pair of tuples: one from the instructor relation and one
from the teaches relation (see next slide)
▪ Since the instructor ID appears in both relations we distinguish between these attribute by attaching to the
attribute the name of the relation from which the attribute originally came.
• instructor.ID
• teaches.ID
C A R T E S I A N - P R O D U C T O P E R AT I O N
C A R T E S I A N - P R O D U C T O P E R AT I O N - E X A M P L E
EXAMPLE QUERY USING CROSS PRODUCT
EXAMPLE –CROSS PRODUCT
CARTESIAN -PRODUCT OPERATION -EXAMPLE
Composition of Relational
Operations

▪ The result of a relational-algebra operation is relation and therefore of relational-algebra operations can be
composed together into a relational-algebra expression.
▪ Consider the query -- Find the names of all instructors in the Physics department.

name( dept_name =“Physics” (instructor))

▪ Instead of giving the name of a relation as the argument of the projection operation, we give an expression
that evaluates to a relation.
COMPOSITION OF OPERATIONS
SET OPERATORS ON RELATIONS
Set Operations

▪ Union
▪ Intersection
▪ Difference
SET OPERATIONS
Union Operation
Union Operation

▪ The union operation allows us to combine two relations


▪ Notation: r  s
▪ For r  s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)
▪ Example: to find all courses taught in the Fall 2017 semester, or in the Spring 2018 semester, or in both
course_id ( semester=“Fall” Λ year=2017 (section)) 
course_id ( semester=“Spring” Λ year=2018 (section))
Union Operation
Set-Intersection Operation

▪ The set-intersection operation allows us to find tuples that are in both the input relations.
▪ Notation: r  s
▪ Assume:
• r, s have the same arity
• attributes of r and s are compatible
▪ Example: Find the set of all courses taught in both the Fall 2017 and the Spring 2018 semesters.
course_id ( semester=“Fall” Λ year=2017 (section)) 
course_id ( semester=“Spring” Λ year=2018 (section))

• Result
Set-Intersection Operation
S E T - I N T E R S EC T I O N O P E R AT I O N - E X A M P L E
Set Difference Operation

▪ The set-difference operation allows us to find tuples that are in one relation but are not in another.
▪ Notation r – s
▪ 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
▪ Example: to find all courses taught in the Fall 2017 semester, but not in the Spring 2018 semester
course_id ( semester=“Fall” Λ year=2017 (section)) −
course_id ( semester=“Spring” Λ year=2018 (section))
Set Difference Operation
Set Difference Operation
Example
JOIN OPERATION
Join
• Joins are used to combine the information of two relations which have at least one field in common. It is one
of the most important operators of RDBMS.

• In simple words, a join operation can be defined as a cartesian product followed by selection or projection
operators.

• The different forms of join operators are:


a) Conditional join (Theta Join)
b) Equi-join
c) Natural join
d) Outer join.
THETA JOIN
JOIN OPERATION -EXAMPLES
THETA JOIN -EXAMPLE
EQUI-JOIN
NATURAL JOIN
NATURAL-JOIN OPERATION
NATURAL JOIN –EXAMPLE
NATURAL JOIN OPERATION – EXAMPLE
OUTER JOIN
OUTER JOIN
OUTER JOIN
OUTER JOIN-EXAMPLE
LEFT OUTER JOIN -EXAMPLE
RIGHT OUTER JOIN -EXAMPLE
FULL OUTER JOIN -EXAMPLE
OUTER JOIN Example
DIVISION OPERATOR
DIVISION OPERATOR -EXAMPLE
Example 1:

• Consider the following relational schema:


COURSES (cno, cname)
STUDENTS (rollno, sname, age, year)
REGISTERED_FOR (cno, rollno)
• The underlined attributes indicate the primary keys for the relations. The ‘year’ attribute for the STUDENTS
relation indicates the year in which the student is currently studying (First year, Second year etc.)
a) Write a relational algebra query to print the roll number of students who have registered for cno 322.
b) Write a SQL query to print the age and year of the youngest student in each year.
EXAMPLE QUERIES
EXAMPLE QUERIES

You might also like