Relational Algebra
Relational Algebra
Types of JOIN:
Various forms of join operation are:
A. Inner Joins:
(i). Theta join (ii). EQUI join (iii). Natural join
B. Outer join:
(i). Left Outer Join (ii). Right Outer Join
(iii). Full Outer Join
b. DIVISION ()
Relational Algebra Operations From Set
Theory
UNION ()
R =A B
Relational Algebra Operations From Set
Theory
UNION ()
Exp.
Relation A Relation B
CNAME CSTATUS CNAME CSTATUS
RAJAT GOOD KARAN POOR
RAHUL EXCELLENT RAJAT GOOD
CNAME CSTATUS
R=A B RAJAT GOOD
RAHUL EXCELLENT
KARAN POOR
Relational Algebra Operations From Set
Theory
INTERSECTION ()
R =A B
Relational Algebra Operations From Set
Theory
INTERSECTION ()
Exp.
Relation A Relation B
CNAME CSTATUS CNAME CSTATUS
RAJAT GOOD KARAN POOR
RAHUL EXCELLENT RAJAT GOOD
DIFFERENCE (–)
R=A–B
Relational Algebra Operations From Set
Theory
DIFFERENCE (–)
Exp.
Relation A Relation B
CNAME CSTATUS CNAME CSTATUS
RAJAT GOOD KARAN POOR
RAHUL EXCELLENT RAJAT GOOD
R =A B
Relational Algebra Operations From Set
Theory
CARTESIAN PRODUCT ()
Exp.
Relation A Relation B
NAME AGE JOB LOCATION
KAJAL 32 DEVELOPER CHENNAI
ANIL 40 ANALYST MUMBAI
a. SELECT ()
b. PROJECT ()
c. RENAME ()
SELECT Operator ()
• SELECT operation is used for selecting a subset of the
tuples according to a given selection condition.
Properties:
Properties:
1. The degree of output relation (number of columns present) is
equal to the number of attributes mentioned in the attribute list.
2. Projection operator does not obey commutative property i.e.
π <list2> (π <list1> (R)) ≠ π <list1> (π <list2> (R))
PROJECT Operator ()
Examples: STUDENT ROLL_NO NAME AGE COURSE
1 ABHISHEK 17 BTech
2 AMIT 16 BCA
3 AJEET 17 BTech
4 AKHIL 18 BTech
5 PRASHANT 17 BCA
Query 1: Find out the name and course from STUDENT relation.
Syntax: name, course (student)
AGE
Query 2: Find the age from STUDENT relation.
17
Syntax: age (student) 16
18
RENAME Operator ()
Examples:
Query 1: Rename STUDENT relation to STUDENT1 relation.
Syntax: ρ (STUDENT1, STUDENT)
A. Inner Join:
(i). Theta join (ii). EQUI join (iii). Natural join
B. Outer Join:
(i). Left Outer Join (ii). Right Outer Join
(iii). Full Outer Join
Join(⋈)
• Join is a binary operation which allows us to combine join
product and selection in one single statement.
• Various forms of join operation are: Inner Join & Outer Join.
Inner Join
• INNER JOIN is used to return rows from both tables which
satisfy the given condition.
• It is the most widely used join operation and can be
considered as a default join-type.
• Inner Join further divided into three subtypes:
(i). Theta join (ii). EQUI join (iii). Natural join
Inner Join: Theta Join
• THETA JOIN allows us to merge two tables based on the
condition represented by theta.
• Theta joins work for all comparison operators.
• It is denoted by symbol θ.
• The general case of JOIN operation is called a Theta join.
Syntax: A ⋈θ B
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 = Φ.
Inner Join: Theta Join
Example: STUDENT SUBJECT
SID NAME STD CLASS SUBJECT
1001 AJAY 11 11 MATH
R1 ⋈ R2
Syntax: A⟕B
Outer Join: Left Outer Join (⟕)
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27
4 16 5 125
R1 ⟕ R2
Syntax: A⟖B
Outer Join: Right Outer Join (⟖)
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27
4 16 5 125
R1 ⟖ R2
Syntax: A⟗B
Outer Join: Full Outer Join (⟗)
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27
4 16 5 125
R1 ⟗ R2
Syntax: AB
DIVISION ()
Relation B has 3 tables as:
Example: Pno Pno Pno
Relation A has 1 P1 P2 P1
(c) Find the name of suppliers who supply both Blue & Black parts.
S_name, color (SUPPLIER) ⋈ (CATALOG ⋈ PARTS) ÷
color (σ color = ‘Blue’ or color = ‘Black’ (PARTS))
Exercise
Example 2: Consider a database that has the relation schema CR
(StudentName, CourseName). An instance of the schema CR is
as given below.
The following queries are made on the database.
T1← πCourseName(σStudentName=‘SA’(CR))
T2← CR ÷ T1
The number of rows in T2 are ______.
(GATE 2017)
Exercise
Solution:
T1 will give: CourseName
CA
CB
CC
T2CR÷T1 = All the tuples in CR which are matched with every tuple in T1:
StudentName
SA
SC
SD
SF
Reference Books