SlideShare a Scribd company logo
1 of 55
Learning Resource
On
Database Management Systems
Chapter-6
Relational Algebra
Prepared By:
Kunal Anand, Asst. Professor
SCE, KIIT, DU, Bhubaneswar-24
Lecture Outcome:
• After the completion of this chapter, the students
will be able to:
– Define Relational Algebra
– Identify different types of relational algebra operations
– Explain different types of JOIN operations
– Write queries using relational algebra
16 March 2021 2
Organization of this Lecture:
• Introduction
• Unary Relational Algebra Operations
• Relational Algebra Operations from Set Theory
• Binary Relational Algebra Operations
• Generalized Projection
• Aggregate Functions
• JOIN Operations
16 March 2021 3
Introduction
• Query Language: The language in which a user requests
information from the database.
• Generally, a query language is of following types:
•Procedural language
•Nonprocedural language
• The categories of different languages are:
•SQL
•Relational Algebra
•Relational Calculus
•Tuple Relational Calculus
•Domain Relational Calculus
16 March 2021 4
Relational Algebra
• Relational Algebra is a procedural language used to
manipulate relations i.e. these operations use one or two
existing relations to create a new relation.
• These operations enable a user to specify basic retrieval
requests as relational algebra expressions.
• The result of the retrieval query is a new relation. The algebra
operations thus produce new relations, that can be further
manipulated using operations of the same algebra.
• A sequence of relational algebra operations forms a relational
algebra expression.
16 March 2021 5
contd..
• The relational algebra is very important because of mainly two
reasons as listed below:
– It provides the formal foundation for relational model
operations.
– It provides basis for implementing and optimizing queries
in the query processing and optimizing modules that are the
integral part of RDBMS.
• Although, most commercial RDBMSs in use today do not
provide user interfaces for relational algebra queries, the core
operations and functions in the internal modules of most
relational systems are based on relational algebra operations.
16 March 2021 6
Relational Algebra Operations
• The operations in relational algebra can be divided into
following three groups:
– Unary RA Operations
• SELECT (symbol: σ)
• PROJECT (symbol: π)
• RENAME (symbol: )
– RA Operations From Set Theory
• UNION (υ)
• INTERSECTION ( ),
• DIFFERENCE (-)
• CARTESIAN PRODUCT ( x )
– Binary RA Operations
• JOIN
• DIVISION
• ASSIGNMENT
16 March 2021 7
Unary RA Operations
• SELECT Operation (σ)
– SELECT operation is used to create a relation from another
relation by selecting only those tuples or rows from the
original relation that satisfy a specified condition.
– It is denoted by sigma (σ) symbol. The predicate appears as
a subscript to σ .
– The argument relation is in parenthesis after the σ. The
result is a relation that has the same attributes as the relation
specified in <relation-name>.
– The general syntax of select operation is:
σ <selection−condition> (<relation name>)
16 March 2021 8
contd..
• The operators used in selection predicate are =, !=, <, ≤, >, ≥ .
• Different predicates can be combined into a larger predicate by
using the connectors like: AND, OR, NOT.
• Examples:
– Query 1: σ(Branch_name='BhubaneswarMain') (LOAN)
Âť SQL: select *
from LOAN
where (Branch_name='BhubaneswarMain')
Âť Output: Tuples where branch_name is BhubaneswarMain
– Query 2: σSubject = 'database' AND Price = 450
(BOOK)
Âť SQL: select *
from Books
where Subject = 'database' AND Price = 450;
Âť Output: Tuples where the subject is database and price is 450.
16 March 2021 9
contd..
– Query 3: σDno=4 OR Salary>25000
(EMPLOYEE)
Âť SQL: select *
from EMPLOYEE
where Dno = 4 OR Salary>25000;
Âť Output: Selects tuples from employee table where either Dno
is 4 or salary > 25000
– Query 4: σSubject = "database" AND Price = 450 or Year
> 2010
(BOOK)
Âť SQL: select *
from Books
where ((Subject = 'database' AND Price =
450) OR Year>2010) ;
Âť Output: Selects tuples from books where subject is 'database'
and price is 450 or those books published after 2010.
16 March 2021 10
Unary RA Operations
• PROJECT Operation (π):
– The PROJECT operation selects certain columns from the
table and discards the other columns.
– The projection method defines a relation that contains a
vertical subset of Relation.
– If the attribute list include only non-key attributes,
duplicate tuples are likely to occur. The PROJECT
operation removes any duplicate tuples and, hence produce
a valid relation which is a set of distinct tuples. This is
known as duplicate elimination.
– The general syntax of project operator is:
•π <attribute−list >
(<relation name>)
16 March 2021 11
contd..
• Examples
– Query 1: π Loan_no,Amount
(LOAN)
• SQL: select Loan_no, Amount from LOAN;
• Output: Displays the columns Loan_no and Amount
from the table LOAN.
– Query 2: πName, Age
(STUDENT)
• SQL: select Name, Age from STUDENT;
• Output: Displays name and age columns from the table
student.
16 March 2021 12
contd..
– Query 3: πSubject, Author
(BOOK)
• SQL: select Subject, Author from BOOK;
• Output: Displays the columns subject and author from
the table Books.
– Query 4: πGender, Salary
(EMPLOYEE)
• SQL: select Gender, Salary from EMPLOYEE;
• Output: Displays the columns Gender and Salary from
the table EMPLOYEE.
16 March 2021 13
Unary RA Operations
Composition of Relational Operators
– Relational algebra operators can be composed together into
a relational algebra expression to answer the complex
queries.
– Relational algebra expression is actually a nesting of
relational operations.
• Query 5:
– π Cust _name (σ Cust _city ='Bhubaneswar '
(CUSTOMER))
– Output: Displays the column cust_name for all those
customers who are from the cust_city Bhubaneswar.
16 March 2021 14
Unary RA Operations
• RENAME Operation (ρ)
– The results of relational algebra expressions do not have a name
that can be used to refer them.
– It is useful to be able to give them names; the rename operator is
used for this purpose.
– It is denoted by rho(ρ) symbol. The general syntax of rename
operator is: ρ X
(E)
• Example:
π cust _name (σ cust _city ='Bhubaneswar '
(Customer)) can be
written as:
1. ρ Customer _Bhubaneswar (σ cust _city=Bhubaneswar
(Customer))
2. π cust _name
(Customer_Bhubaneswar)
16 March 2021 15
Union Compatibility
• To perform the set operations such as UNION,
INTERSECTION, and SET DIFFERENCE (also known as
MINUS or EXCEPT) the relations need to be union
compatible for the result to be a valid relation.
• Two relations R1(a1,a2,... an ) and R2(b1,b2,... bm ) are union
compatible iff:
•n = m, i.e. both relations have same degree
•dom(ai ) = dom(bi ) for 1 ≤ i ≤n
– This mean that the two relations have the same number of
attributes.
– Each corresponding pair of attributes has the same domain.
16 March 2021 16
RA Operations from Set Theory
• UNION Operation(∪):
– The union operation is used to combine data from two
relations. It is denoted by union(∪) symbol.
– The union of two relations R1(a1,a2,... an ) and R2 (b1,b2,...
bn ) is a relation R3 (c1,c ,... cn ) such that:
• dom(ci ) = dom(ai ) ∪ dom(bi ), 1 ≤ i ≤ n
– R1 ∪ R2 is a relation that includes all tuples that are either
present in R1 or R2 or in both without duplicate tuples.
16 March 2021 17
Examples
– Query 1: ∏ Cust_name
(DEPOSITOR) ∪∏ Cust_name
(BORROWER)
• SQL: select Cust_name from DEPOSITOR
union
select Cust_name from BORROWER
16 March 2021 18
contd..
• Intersection Operation (∩)
– The intersection operation is used to identify the rows that
are common to two relations.
– An intersection is defined by the symbol ∩
– The intersection of two relations R1(a1,a2,... an ) and
R2(b1,b2,... bn) is a relation R3 (c1,c2,... cn ) such that:
• dom(ci ) = dom(ai ) ∩ dom(bi ), 1 ≤ i ≤ n
– R1 ∩ R2 is a relation that includes all tuples that are present
in both R1 and R2
16 March 2021 19
contd..
– Query 1: ∏ cust_name
(Depositor) ∩ ∏ cust_name
(Borrower)
• SQL: select Cust_name from DEPOSITOR
intersect
select Cust_name from BORROWER
16 March 2021 20
Cust_name
Mahesh
Ramesh
Rishi
contd..
• DIFFERENCE Operation (-)
– The difference operation is used to identify the rows that
are in one relation and not in another. It is denoted as (-)
symbol.
– The difference of two relations R1(a1,a2,... an ) and
R2(b1,b2,... bn ) is a relation R3 (c1,c2,... cn ) such that:
• dom(ci ) = dom(ai ) - dom(bi ), 1 ≤ i ≤ n
– R1 - R2 is a relation that includes all tuples that are in R1, but
not in R2
– Query 1: ∏ author
(Books) − ∏ author
(Articles)
• Output − Provides the name of authors who have written books but
not articles.
– Query 2: ∏Student
(RegularClass) - ∏Student
(ExtraClass)
• Output: Provide the name of students who attend the regular
class but not the extra class.
16 March 2021 21
An Example
16 March 2021 22
Cartesian Product Operation (×)
• It is also known as Cross Product. Also a binary operation, but
the relations on which it is applied do not have to be union
compatible.
• The Cartesian product of two relations R1(a1,a2,... an ) with
cardinality i and R2(b1,b2,... bm ) with cardinality j is a relation
R3 with
•degree k = n + m,
•cardinality i*j and
•attributes (a1,a2,... an , b1,b2,... bm )
• R1 × R2 is a relation that includes all the possible combinations
of tuples from R1 and R2 .
• The Cartesian product is used to combine information from any
two relations. It is not a useful operation by itself; but is used
in conjunction with other operations.
16 March 2021 23
contd..
16 March 2021 24
contd..
16 March 2021 25
Binary RA Operation
Division Operation (/)
• Produces the tuples in one relation, R, that match all tuples in
another relation, S
– R (A1, …An, B1, …Bm)
– S (B1 …Bm)
– R/S, with attributes A1, …An, is the set of all tuples <a>
such that for every tuple <b> in S, <a,b> is in R
• Can be expressed in terms of projection, set difference, and
cross-product.
• The division operator is used when we have to evaluate
queries which contain the keyword ALL.
16 March 2021 26
contd..
16 March 2021 27
Assignment Operator(←)
• It works like assignment in a programming language.
• In relational algebra, the assignment operator gives a name to a
relation. It is denoted by (←) symbol.
• The result of the right of the ← symbol is assigned to the
relation variable on the left of the ← symbol.
• Assignment must always be made to a temporary relation
variable.
• With the assignment operator, a query can be written as a
sequential program consisting of:
•a series of assignment,
•followed by an expression whose value is displayed as a
result of the query.
16 March 2021 28
contd..
• Example:
Temp1 ← π cust _name,branch_name
(Depositor_Account)
Temp2← πbranch_name (σ branch_city =”Mumbai”(Branch))
Result = Temp1 á Temp2
Output: The result table will have the customers
Who have deposits in all branches in Mumbai
city.
16 March 2021 29
Generalized Projection
• The generalized-projection operation extends the projection
operation by allowing arithmetic functions to be used in the
projection list. The general form of generalized-projection is:
π F1 ,F2 ...Fn (E)
• Ex:
– Emp(ssn, salary, deduction, years_service)
– A report may be required to show
• net_salary=salary-deduction,
• bonus=2000*years_service and
• tax=0.25*salary
• REPORT ← ρ
(ssn,net _salary,bonus,tax ) (π ssn,salary
−deduction,2000∗years_service,0.25∗salary
(EMPLOYEE))
16 March 2021 30
Aggregate Functions(g)
• Aggregate functions take a collection of values and return a
single value as a result.
• NULL value will not participate in the aggregate functions.
• The general form of aggregate function is: grouping_attribute
gaggregate_functions (R)
• Ex: Works(emp_id, ename, salary, branch_name)
• Query: Find the total sum of salaries of all the employees
• Ans: g SUM(salary )(Works)
16 March 2021 31
contd..
• Query: Find the total sum of salaries of all the employees in
each branch
Ans: branch_name g SUM(salary )
(Works)
• Query: Find the maximum salary for the employees at each
branch, in addition to the sum of the salaries
Ans: branch_name g SUM(salary ),MAX (salary )
(Works)
• Query: Find the number of employees working
Ans: g COUNT (emp_id )
(Works)
16 March 2021 32
Join Operation
• Join is a combination of a Cartesian product followed by a
selection process.
• A Join operation pairs two tuples from different relations, if
and only if a given join condition is satisfied.
• It is denoted by ⋈.
• Different categories of join are:
•Cross Join (Cartesian Product)
•Inner Join
•Outer Join
•Self Join
16 March 2021 33
Inner Join
• These joins are the one that has the tuples that satisfy some
conditions and rest are discarded. Further they are classified as
– Theta join:
• They have tuples from different relations if and only if
they satisfy the theta condition.
• R1 and R2 are relations having attributes (A1, A2, ..,
An) and (B1, B2,.. ,Bn) such that the attributes don’t
have anything in common, that is R1 ∊ R2 = Ό.
• It is denoted by R1 ⋈θ R2;
• Here the comparison operators (≤, ≥, ˂, ˃, =, ̚ )come
into picture.
• Degree (Result) = Degree (R) + Degree (S)
• Cardinality (Result) ≤ Cardinality(R) × Cardinality(S)
16 March 2021 34
An Example
• Suppose a customer wants to buy a car and a boat. But, they
don’t want to spend more money on a boat than a car i.e. car
price >= boat price. Display the car model, car price, boat
model, boat price for the customer.
• SQL: select *
from car JOIN boat
on car.car_price>=boat.boat_price;
• Query: π car_model, car_price, boat_model, boat_price
( σ car.car_price>=boat.boat_price
(car X boat))
16 March 2021 35
contd..
16 March 2021 36
Inner Join (contd..)
• Equi Join
– A special theta join based on equality of specified columns.
– The equi join is the special type of theta join where the
comparison operator is =
– It is denoted by R1 ⋈= R2
– Example: Display the student id, student name, and course
name for the students from the same courses.
– SQL: select Student.sid, Student.name, Courses.name
from Student JOIN Courses
on Student.course=Courses.course;
–Query: π sid, student.name , Courses.name
( σ Student.course=Courses.course
(student X courses))
16 March 2021 37
contd..
16 March 2021 38
Inner Join (contd..)
• Natural Join
– Natural Join is a type of Inner join which is based on
column having same name and same datatype present in
both the tables to be joined. Natural join does not use any
comparison operator.
– Natural Join is by default inner join because the tuples
which does not satisfy the conditions of join does not
appear in result set.
– Example: Display the details of the students along with
their address.
• SQL:
– select * from class NATURAL JOIN classinfo;
• Query: class ⋈classinfo
16 March 2021 39
An Example
16 March 2021 40
Outer Join Operations
• An inner join includes only those tuples with matching
attributes and the rest are discarded in the resulting relation.
Therefore, we need to use outer joins to include all the tuples
from the participating relations in the resulting relation.
• It is an extension of the natural join operation to deal with the
missing information.
• The different types of outer join are:
•Left Outer Join
•Right Outer Join
•Full Outer Join
16 March 2021 41
Left Outer Join(R⟕S)
• The left outer join preserves all tuples in left relation. All
information from the left relation is present in the result of the
left outer join.
• The tuples of R which do not satisfy join condition will have
values as NULL for attributes of S.
• Example: Consider the relations Instructor and Teaches. Find
the left outer join of them.
• SQL: select *
from instructor LEFT OUTER JOIN teaches
on instructor.ID=teaches.ID;
• Query: instructor ⟕ teaches
16 March 2021 42
contd..
16 March 2021 43
Left Outer Join (instructor ⟕ teaches)
16 March 2021 44
Right Outer Join(R ⟖ S)
• The right outer join preserves all tuples in right relation. All
information from the right relation is present in the result of the
right outer join.
• The tuples of S which do not satisfy join condition will have
values as NULL for attributes of R.
• Example: Consider the relations Instructor and Teaches. Find
the right outer join of them.
• SQL: select *
from teaches RIGHT OUTER JOIN instructor
on teaches.ID = instructor.ID;
• Query: teaches ⟖ instructor
16 March 2021 45
contd..
16 March 2021 46
Full Outer Join (R ⟗ S)
• All the tuples from both participating relations are included in
the resulting relation. If there are no matching tuples for both
relations, their respective unmatched attributes are made
NULL.
• Example: Select students whose ROLL_NO is greater than
EMP_NO of employees and details of other Employees as
well and other Students as well.
• SQL: select*
from student FULL OUTER JOIN employee
on student.roll_no>employee.emp_no;
• QUERY:
– STUDENT⟗STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE
16 March 2021 47
contd..
16 March 2021 48
contd..
16 March 2021 49
Left Outer / Right Outer Join
16 March 2021 50
16 March 2021 51
Full Outer Join
Self Join
• A self join is a join in which a table is joined with itself (which
is also called Unary relationships), especially when the table
has a FOREIGN KEY which references its own PRIMARY
KEY.
• To join a table itself means that each row of the table is
combined with itself and with every other row of the table.
• The self join can be viewed as a join of two copies of the same
table. The table is not actually copied, but SQL performs the
command as though it were.
• The syntax of the command for joining a table to itself is
almost same as that for joining two different tables.
– SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
16 March 2021 52
An Example
• Suppose a table EMPLOYEE has been created with the
attributes Emp_ID, Emp_name, dt_of_join, Emp_supv.
• Here, Emp_ID is the PK as well as the FK as the Emp_supv is
also an employee. The FK is used to the refer the PK of the
same table in self join operation.
• Now, If we want a list of employees and the names of their
supervisors, we’ll have to JOIN the EMPLOYEE table to itself
to get this list.
16 March 2021 53
contd..
16 March 2021 54
contd..
• SQL:
– SELECT a.emp_id AS "Emp_ID",
a.emp_name AS "Employee Name",
b.emp_id AS "Supervisor ID",
b.emp_name AS "Supervisor Name"
FROM employee a, employee b
WHERE a.emp_supv = b.emp_id;
16 March 2021 55

More Related Content

What's hot

Unit 4 plsql
Unit 4  plsqlUnit 4  plsql
Unit 4 plsqlDrkhanchanaR
 
Keys in dbms
Keys in dbmsKeys in dbms
Keys in dbmsshalini s
 
MySQL Performance for DevOps
MySQL Performance for DevOpsMySQL Performance for DevOps
MySQL Performance for DevOpsSveta Smirnova
 
PostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingPostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingAmir Reza Hashemi
 
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAINSyahriha Ruslan
 
Normalization in SQL | Edureka
Normalization in SQL | EdurekaNormalization in SQL | Edureka
Normalization in SQL | EdurekaEdureka!
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFOum Saokosal
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQLCoT
 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalizationdaxesh chauhan
 
Oracle pl/sql control statments
Oracle pl/sql control statmentsOracle pl/sql control statments
Oracle pl/sql control statmentsTayba Bashir
 
Relational model
Relational modelRelational model
Relational modelWBUTTUTORIALS
 
joins in database
 joins in database joins in database
joins in databaseSultan Arshad
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | EdurekaEdureka!
 
Key and its different types
Key and its different typesKey and its different types
Key and its different typesUmair Shakir
 
Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Shriya agrawal
 

What's hot (20)

Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Unit 4 plsql
Unit 4  plsqlUnit 4  plsql
Unit 4 plsql
 
Keys in dbms
Keys in dbmsKeys in dbms
Keys in dbms
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
MySQL Performance for DevOps
MySQL Performance for DevOpsMySQL Performance for DevOps
MySQL Performance for DevOps
 
Sql joins
Sql joinsSql joins
Sql joins
 
Normalization
NormalizationNormalization
Normalization
 
PostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / ShardingPostgreSQL Table Partitioning / Sharding
PostgreSQL Table Partitioning / Sharding
 
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
Normalization in SQL | Edureka
Normalization in SQL | EdurekaNormalization in SQL | Edureka
Normalization in SQL | Edureka
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQL
 
Functional dependencies and normalization
Functional dependencies and normalizationFunctional dependencies and normalization
Functional dependencies and normalization
 
Oracle pl/sql control statments
Oracle pl/sql control statmentsOracle pl/sql control statments
Oracle pl/sql control statments
 
Relational model
Relational modelRelational model
Relational model
 
joins in database
 joins in database joins in database
joins in database
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
Key and its different types
Key and its different typesKey and its different types
Key and its different types
 
Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf
 

Similar to Chapter-6 Relational Algebra

Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational modelATS SBGI MIRAJ
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbmsVignesh Saravanan
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization Hafiz faiz
 
Dbms 11: Relational Algebra
Dbms 11: Relational AlgebraDbms 11: Relational Algebra
Dbms 11: Relational AlgebraAmiya9439793168
 
Relational algebra calculus
Relational algebra  calculusRelational algebra  calculus
Relational algebra calculusVaibhav Kathuria
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)Mukund Trivedi
 
Relational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfRelational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfChristalin Nelson
 
Relational operation final
Relational operation finalRelational operation final
Relational operation finalStudent
 
Introduction to Relational Database Management Systems
Introduction to Relational Database Management SystemsIntroduction to Relational Database Management Systems
Introduction to Relational Database Management SystemsAdri Jovin
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica"FENG "GEORGE"" YU
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational AlgebraPyingkodi Maran
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasexabhaysonone0
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Raj vardhan
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational AlgebraAmin Omi
 
Ch7
Ch7Ch7
Ch7muteddy
 
Relational Algebra Operations
Relational Algebra OperationsRelational Algebra Operations
Relational Algebra OperationsShefa Idrees
 

Similar to Chapter-6 Relational Algebra (20)

Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Query Decomposition and data localization
Query Decomposition and data localization Query Decomposition and data localization
Query Decomposition and data localization
 
Dbms 11: Relational Algebra
Dbms 11: Relational AlgebraDbms 11: Relational Algebra
Dbms 11: Relational Algebra
 
Relational algebra calculus
Relational algebra  calculusRelational algebra  calculus
Relational algebra calculus
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)
 
Relational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdfRelational_Algebra_Calculus Operations.pdf
Relational_Algebra_Calculus Operations.pdf
 
Relational operation final
Relational operation finalRelational operation final
Relational operation final
 
Introduction to Relational Database Management Systems
Introduction to Relational Database Management SystemsIntroduction to Relational Database Management Systems
Introduction to Relational Database Management Systems
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
 
RDBMS
RDBMSRDBMS
RDBMS
 
354 ch6
354 ch6354 ch6
354 ch6
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Ch7
Ch7Ch7
Ch7
 
Relational Algebra Operations
Relational Algebra OperationsRelational Algebra Operations
Relational Algebra Operations
 

More from Kunal Anand

Chapter-5 The Relational Data Model
Chapter-5 The Relational Data ModelChapter-5 The Relational Data Model
Chapter-5 The Relational Data ModelKunal Anand
 
Chapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error RecoveryChapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error RecoveryKunal Anand
 
Chapter-9 Normalization
Chapter-9 NormalizationChapter-9 Normalization
Chapter-9 NormalizationKunal Anand
 
Chapter-8 Relational Database Design
Chapter-8 Relational Database DesignChapter-8 Relational Database Design
Chapter-8 Relational Database DesignKunal Anand
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational CalculusKunal Anand
 
Chapter-4 Enhanced ER Model
Chapter-4 Enhanced ER ModelChapter-4 Enhanced ER Model
Chapter-4 Enhanced ER ModelKunal Anand
 
Chapter-3 Data Modeling using ER Model
Chapter-3 Data Modeling using ER ModelChapter-3 Data Modeling using ER Model
Chapter-3 Data Modeling using ER ModelKunal Anand
 
Chapter-2 Database System Concepts and Architecture
Chapter-2 Database System Concepts and ArchitectureChapter-2 Database System Concepts and Architecture
Chapter-2 Database System Concepts and ArchitectureKunal Anand
 
Chapter-1 Introduction to Database Management Systems
Chapter-1 Introduction to Database Management SystemsChapter-1 Introduction to Database Management Systems
Chapter-1 Introduction to Database Management SystemsKunal Anand
 

More from Kunal Anand (9)

Chapter-5 The Relational Data Model
Chapter-5 The Relational Data ModelChapter-5 The Relational Data Model
Chapter-5 The Relational Data Model
 
Chapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error RecoveryChapter-10 Transaction Processing and Error Recovery
Chapter-10 Transaction Processing and Error Recovery
 
Chapter-9 Normalization
Chapter-9 NormalizationChapter-9 Normalization
Chapter-9 Normalization
 
Chapter-8 Relational Database Design
Chapter-8 Relational Database DesignChapter-8 Relational Database Design
Chapter-8 Relational Database Design
 
Chapter-7 Relational Calculus
Chapter-7 Relational CalculusChapter-7 Relational Calculus
Chapter-7 Relational Calculus
 
Chapter-4 Enhanced ER Model
Chapter-4 Enhanced ER ModelChapter-4 Enhanced ER Model
Chapter-4 Enhanced ER Model
 
Chapter-3 Data Modeling using ER Model
Chapter-3 Data Modeling using ER ModelChapter-3 Data Modeling using ER Model
Chapter-3 Data Modeling using ER Model
 
Chapter-2 Database System Concepts and Architecture
Chapter-2 Database System Concepts and ArchitectureChapter-2 Database System Concepts and Architecture
Chapter-2 Database System Concepts and Architecture
 
Chapter-1 Introduction to Database Management Systems
Chapter-1 Introduction to Database Management SystemsChapter-1 Introduction to Database Management Systems
Chapter-1 Introduction to Database Management Systems
 

Recently uploaded

Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 

Chapter-6 Relational Algebra

  • 1. Learning Resource On Database Management Systems Chapter-6 Relational Algebra Prepared By: Kunal Anand, Asst. Professor SCE, KIIT, DU, Bhubaneswar-24
  • 2. Lecture Outcome: • After the completion of this chapter, the students will be able to: – Define Relational Algebra – Identify different types of relational algebra operations – Explain different types of JOIN operations – Write queries using relational algebra 16 March 2021 2
  • 3. Organization of this Lecture: • Introduction • Unary Relational Algebra Operations • Relational Algebra Operations from Set Theory • Binary Relational Algebra Operations • Generalized Projection • Aggregate Functions • JOIN Operations 16 March 2021 3
  • 4. Introduction • Query Language: The language in which a user requests information from the database. • Generally, a query language is of following types: •Procedural language •Nonprocedural language • The categories of different languages are: •SQL •Relational Algebra •Relational Calculus •Tuple Relational Calculus •Domain Relational Calculus 16 March 2021 4
  • 5. Relational Algebra • Relational Algebra is a procedural language used to manipulate relations i.e. these operations use one or two existing relations to create a new relation. • These operations enable a user to specify basic retrieval requests as relational algebra expressions. • The result of the retrieval query is a new relation. The algebra operations thus produce new relations, that can be further manipulated using operations of the same algebra. • A sequence of relational algebra operations forms a relational algebra expression. 16 March 2021 5
  • 6. contd.. • The relational algebra is very important because of mainly two reasons as listed below: – It provides the formal foundation for relational model operations. – It provides basis for implementing and optimizing queries in the query processing and optimizing modules that are the integral part of RDBMS. • Although, most commercial RDBMSs in use today do not provide user interfaces for relational algebra queries, the core operations and functions in the internal modules of most relational systems are based on relational algebra operations. 16 March 2021 6
  • 7. Relational Algebra Operations • The operations in relational algebra can be divided into following three groups: – Unary RA Operations • SELECT (symbol: σ) • PROJECT (symbol: π) • RENAME (symbol: ) – RA Operations From Set Theory • UNION (υ) • INTERSECTION ( ), • DIFFERENCE (-) • CARTESIAN PRODUCT ( x ) – Binary RA Operations • JOIN • DIVISION • ASSIGNMENT 16 March 2021 7
  • 8. Unary RA Operations • SELECT Operation (σ) – SELECT operation is used to create a relation from another relation by selecting only those tuples or rows from the original relation that satisfy a specified condition. – It is denoted by sigma (σ) symbol. The predicate appears as a subscript to σ . – The argument relation is in parenthesis after the σ. The result is a relation that has the same attributes as the relation specified in <relation-name>. – The general syntax of select operation is: σ <selection−condition> (<relation name>) 16 March 2021 8
  • 9. contd.. • The operators used in selection predicate are =, !=, <, ≤, >, ≥ . • Different predicates can be combined into a larger predicate by using the connectors like: AND, OR, NOT. • Examples: – Query 1: σ(Branch_name='BhubaneswarMain') (LOAN) Âť SQL: select * from LOAN where (Branch_name='BhubaneswarMain') Âť Output: Tuples where branch_name is BhubaneswarMain – Query 2: σSubject = 'database' AND Price = 450 (BOOK) Âť SQL: select * from Books where Subject = 'database' AND Price = 450; Âť Output: Tuples where the subject is database and price is 450. 16 March 2021 9
  • 10. contd.. – Query 3: σDno=4 OR Salary>25000 (EMPLOYEE) Âť SQL: select * from EMPLOYEE where Dno = 4 OR Salary>25000; Âť Output: Selects tuples from employee table where either Dno is 4 or salary > 25000 – Query 4: σSubject = "database" AND Price = 450 or Year > 2010 (BOOK) Âť SQL: select * from Books where ((Subject = 'database' AND Price = 450) OR Year>2010) ; Âť Output: Selects tuples from books where subject is 'database' and price is 450 or those books published after 2010. 16 March 2021 10
  • 11. Unary RA Operations • PROJECT Operation (π): – The PROJECT operation selects certain columns from the table and discards the other columns. – The projection method defines a relation that contains a vertical subset of Relation. – If the attribute list include only non-key attributes, duplicate tuples are likely to occur. The PROJECT operation removes any duplicate tuples and, hence produce a valid relation which is a set of distinct tuples. This is known as duplicate elimination. – The general syntax of project operator is: •π <attribute−list > (<relation name>) 16 March 2021 11
  • 12. contd.. • Examples – Query 1: π Loan_no,Amount (LOAN) • SQL: select Loan_no, Amount from LOAN; • Output: Displays the columns Loan_no and Amount from the table LOAN. – Query 2: πName, Age (STUDENT) • SQL: select Name, Age from STUDENT; • Output: Displays name and age columns from the table student. 16 March 2021 12
  • 13. contd.. – Query 3: πSubject, Author (BOOK) • SQL: select Subject, Author from BOOK; • Output: Displays the columns subject and author from the table Books. – Query 4: πGender, Salary (EMPLOYEE) • SQL: select Gender, Salary from EMPLOYEE; • Output: Displays the columns Gender and Salary from the table EMPLOYEE. 16 March 2021 13
  • 14. Unary RA Operations Composition of Relational Operators – Relational algebra operators can be composed together into a relational algebra expression to answer the complex queries. – Relational algebra expression is actually a nesting of relational operations. • Query 5: – π Cust _name (σ Cust _city ='Bhubaneswar ' (CUSTOMER)) – Output: Displays the column cust_name for all those customers who are from the cust_city Bhubaneswar. 16 March 2021 14
  • 15. Unary RA Operations • RENAME Operation (ρ) – The results of relational algebra expressions do not have a name that can be used to refer them. – It is useful to be able to give them names; the rename operator is used for this purpose. – It is denoted by rho(ρ) symbol. The general syntax of rename operator is: ρ X (E) • Example: π cust _name (σ cust _city ='Bhubaneswar ' (Customer)) can be written as: 1. ρ Customer _Bhubaneswar (σ cust _city=Bhubaneswar (Customer)) 2. π cust _name (Customer_Bhubaneswar) 16 March 2021 15
  • 16. Union Compatibility • To perform the set operations such as UNION, INTERSECTION, and SET DIFFERENCE (also known as MINUS or EXCEPT) the relations need to be union compatible for the result to be a valid relation. • Two relations R1(a1,a2,... an ) and R2(b1,b2,... bm ) are union compatible iff: •n = m, i.e. both relations have same degree •dom(ai ) = dom(bi ) for 1 ≤ i ≤n – This mean that the two relations have the same number of attributes. – Each corresponding pair of attributes has the same domain. 16 March 2021 16
  • 17. RA Operations from Set Theory • UNION Operation(∪): – The union operation is used to combine data from two relations. It is denoted by union(∪) symbol. – The union of two relations R1(a1,a2,... an ) and R2 (b1,b2,... bn ) is a relation R3 (c1,c ,... cn ) such that: • dom(ci ) = dom(ai ) ∪ dom(bi ), 1 ≤ i ≤ n – R1 ∪ R2 is a relation that includes all tuples that are either present in R1 or R2 or in both without duplicate tuples. 16 March 2021 17
  • 18. Examples – Query 1: ∏ Cust_name (DEPOSITOR) ∪∏ Cust_name (BORROWER) • SQL: select Cust_name from DEPOSITOR union select Cust_name from BORROWER 16 March 2021 18
  • 19. contd.. • Intersection Operation (∊) – The intersection operation is used to identify the rows that are common to two relations. – An intersection is defined by the symbol ∊ – The intersection of two relations R1(a1,a2,... an ) and R2(b1,b2,... bn) is a relation R3 (c1,c2,... cn ) such that: • dom(ci ) = dom(ai ) ∊ dom(bi ), 1 ≤ i ≤ n – R1 ∊ R2 is a relation that includes all tuples that are present in both R1 and R2 16 March 2021 19
  • 20. contd.. – Query 1: ∏ cust_name (Depositor) ∊ ∏ cust_name (Borrower) • SQL: select Cust_name from DEPOSITOR intersect select Cust_name from BORROWER 16 March 2021 20 Cust_name Mahesh Ramesh Rishi
  • 21. contd.. • DIFFERENCE Operation (-) – The difference operation is used to identify the rows that are in one relation and not in another. It is denoted as (-) symbol. – The difference of two relations R1(a1,a2,... an ) and R2(b1,b2,... bn ) is a relation R3 (c1,c2,... cn ) such that: • dom(ci ) = dom(ai ) - dom(bi ), 1 ≤ i ≤ n – R1 - R2 is a relation that includes all tuples that are in R1, but not in R2 – Query 1: ∏ author (Books) − ∏ author (Articles) • Output − Provides the name of authors who have written books but not articles. – Query 2: ∏Student (RegularClass) - ∏Student (ExtraClass) • Output: Provide the name of students who attend the regular class but not the extra class. 16 March 2021 21
  • 23. Cartesian Product Operation (×) • It is also known as Cross Product. Also a binary operation, but the relations on which it is applied do not have to be union compatible. • The Cartesian product of two relations R1(a1,a2,... an ) with cardinality i and R2(b1,b2,... bm ) with cardinality j is a relation R3 with •degree k = n + m, •cardinality i*j and •attributes (a1,a2,... an , b1,b2,... bm ) • R1 × R2 is a relation that includes all the possible combinations of tuples from R1 and R2 . • The Cartesian product is used to combine information from any two relations. It is not a useful operation by itself; but is used in conjunction with other operations. 16 March 2021 23
  • 26. Binary RA Operation Division Operation (/) • Produces the tuples in one relation, R, that match all tuples in another relation, S – R (A1, …An, B1, …Bm) – S (B1 …Bm) – R/S, with attributes A1, …An, is the set of all tuples <a> such that for every tuple <b> in S, <a,b> is in R • Can be expressed in terms of projection, set difference, and cross-product. • The division operator is used when we have to evaluate queries which contain the keyword ALL. 16 March 2021 26
  • 28. Assignment Operator(←) • It works like assignment in a programming language. • In relational algebra, the assignment operator gives a name to a relation. It is denoted by (←) symbol. • The result of the right of the ← symbol is assigned to the relation variable on the left of the ← symbol. • Assignment must always be made to a temporary relation variable. • With the assignment operator, a query can be written as a sequential program consisting of: •a series of assignment, •followed by an expression whose value is displayed as a result of the query. 16 March 2021 28
  • 29. contd.. • Example: Temp1 ← π cust _name,branch_name (Depositor_Account) Temp2← πbranch_name (σ branch_city =”Mumbai”(Branch)) Result = Temp1 á Temp2 Output: The result table will have the customers Who have deposits in all branches in Mumbai city. 16 March 2021 29
  • 30. Generalized Projection • The generalized-projection operation extends the projection operation by allowing arithmetic functions to be used in the projection list. The general form of generalized-projection is: π F1 ,F2 ...Fn (E) • Ex: – Emp(ssn, salary, deduction, years_service) – A report may be required to show • net_salary=salary-deduction, • bonus=2000*years_service and • tax=0.25*salary • REPORT ← ρ (ssn,net _salary,bonus,tax ) (π ssn,salary −deduction,2000∗years_service,0.25∗salary (EMPLOYEE)) 16 March 2021 30
  • 31. Aggregate Functions(g) • Aggregate functions take a collection of values and return a single value as a result. • NULL value will not participate in the aggregate functions. • The general form of aggregate function is: grouping_attribute gaggregate_functions (R) • Ex: Works(emp_id, ename, salary, branch_name) • Query: Find the total sum of salaries of all the employees • Ans: g SUM(salary )(Works) 16 March 2021 31
  • 32. contd.. • Query: Find the total sum of salaries of all the employees in each branch Ans: branch_name g SUM(salary ) (Works) • Query: Find the maximum salary for the employees at each branch, in addition to the sum of the salaries Ans: branch_name g SUM(salary ),MAX (salary ) (Works) • Query: Find the number of employees working Ans: g COUNT (emp_id ) (Works) 16 March 2021 32
  • 33. Join Operation • Join is a combination of a Cartesian product followed by a selection process. • A Join operation pairs two tuples from different relations, if and only if a given join condition is satisfied. • It is denoted by ⋈. • Different categories of join are: •Cross Join (Cartesian Product) •Inner Join •Outer Join •Self Join 16 March 2021 33
  • 34. Inner Join • These joins are the one that has the tuples that satisfy some conditions and rest are discarded. Further they are classified as – Theta join: • They have tuples from different relations if and only if they satisfy the theta condition. • R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the attributes don’t have anything in common, that is R1 ∊ R2 = ÎŚ. • It is denoted by R1 ⋈θ R2; • Here the comparison operators (≤, ≥, ˂, ˃, =, ̚ )come into picture. • Degree (Result) = Degree (R) + Degree (S) • Cardinality (Result) ≤ Cardinality(R) × Cardinality(S) 16 March 2021 34
  • 35. An Example • Suppose a customer wants to buy a car and a boat. But, they don’t want to spend more money on a boat than a car i.e. car price >= boat price. Display the car model, car price, boat model, boat price for the customer. • SQL: select * from car JOIN boat on car.car_price>=boat.boat_price; • Query: π car_model, car_price, boat_model, boat_price ( σ car.car_price>=boat.boat_price (car X boat)) 16 March 2021 35
  • 37. Inner Join (contd..) • Equi Join – A special theta join based on equality of specified columns. – The equi join is the special type of theta join where the comparison operator is = – It is denoted by R1 ⋈= R2 – Example: Display the student id, student name, and course name for the students from the same courses. – SQL: select Student.sid, Student.name, Courses.name from Student JOIN Courses on Student.course=Courses.course; –Query: π sid, student.name , Courses.name ( σ Student.course=Courses.course (student X courses)) 16 March 2021 37
  • 39. Inner Join (contd..) • Natural Join – Natural Join is a type of Inner join which is based on column having same name and same datatype present in both the tables to be joined. Natural join does not use any comparison operator. – Natural Join is by default inner join because the tuples which does not satisfy the conditions of join does not appear in result set. – Example: Display the details of the students along with their address. • SQL: – select * from class NATURAL JOIN classinfo; • Query: class ⋈classinfo 16 March 2021 39
  • 41. Outer Join Operations • An inner join includes only those tuples with matching attributes and the rest are discarded in the resulting relation. Therefore, we need to use outer joins to include all the tuples from the participating relations in the resulting relation. • It is an extension of the natural join operation to deal with the missing information. • The different types of outer join are: •Left Outer Join •Right Outer Join •Full Outer Join 16 March 2021 41
  • 42. Left Outer Join(R⟕S) • The left outer join preserves all tuples in left relation. All information from the left relation is present in the result of the left outer join. • The tuples of R which do not satisfy join condition will have values as NULL for attributes of S. • Example: Consider the relations Instructor and Teaches. Find the left outer join of them. • SQL: select * from instructor LEFT OUTER JOIN teaches on instructor.ID=teaches.ID; • Query: instructor ⟕ teaches 16 March 2021 42
  • 44. Left Outer Join (instructor ⟕ teaches) 16 March 2021 44
  • 45. Right Outer Join(R ⟖ S) • The right outer join preserves all tuples in right relation. All information from the right relation is present in the result of the right outer join. • The tuples of S which do not satisfy join condition will have values as NULL for attributes of R. • Example: Consider the relations Instructor and Teaches. Find the right outer join of them. • SQL: select * from teaches RIGHT OUTER JOIN instructor on teaches.ID = instructor.ID; • Query: teaches ⟖ instructor 16 March 2021 45
  • 47. Full Outer Join (R ⟗ S) • All the tuples from both participating relations are included in the resulting relation. If there are no matching tuples for both relations, their respective unmatched attributes are made NULL. • Example: Select students whose ROLL_NO is greater than EMP_NO of employees and details of other Employees as well and other Students as well. • SQL: select* from student FULL OUTER JOIN employee on student.roll_no>employee.emp_no; • QUERY: – STUDENT⟗STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE 16 March 2021 47
  • 50. Left Outer / Right Outer Join 16 March 2021 50
  • 51. 16 March 2021 51 Full Outer Join
  • 52. Self Join • A self join is a join in which a table is joined with itself (which is also called Unary relationships), especially when the table has a FOREIGN KEY which references its own PRIMARY KEY. • To join a table itself means that each row of the table is combined with itself and with every other row of the table. • The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were. • The syntax of the command for joining a table to itself is almost same as that for joining two different tables. – SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE a.common_filed = b.common_field; 16 March 2021 52
  • 53. An Example • Suppose a table EMPLOYEE has been created with the attributes Emp_ID, Emp_name, dt_of_join, Emp_supv. • Here, Emp_ID is the PK as well as the FK as the Emp_supv is also an employee. The FK is used to the refer the PK of the same table in self join operation. • Now, If we want a list of employees and the names of their supervisors, we’ll have to JOIN the EMPLOYEE table to itself to get this list. 16 March 2021 53
  • 55. contd.. • SQL: – SELECT a.emp_id AS "Emp_ID", a.emp_name AS "Employee Name", b.emp_id AS "Supervisor ID", b.emp_name AS "Supervisor Name" FROM employee a, employee b WHERE a.emp_supv = b.emp_id; 16 March 2021 55