DBMS_07-08
DBMS_07-08
1
1/21/2025
Procedural Non-Procedural
Relational Relational
Algebra Calculus
SQL
Department of CSE, National Institute of Technology Rourkela
2
1/21/2025
Relational Algebra
3
1/21/2025
Relational Algebra
It is just like a simple operation; instead of number or
variables as an operand we will have relation as
operand).
8 + 9
A&B
Operations
Basic/Fundamental Operations:
• Unary Operations:
• Selection (σ): Selects a subset of tuples / rows from a relation
• Projection (π): Retains a subset of columns from a relation
• Rename (ρ): Renames a relation
• Binary operations:
• Cross Product / Cartesian Product (×): Allows to combine two
relations
• Difference / Set difference (−): Returns tuples present in one
relation but not in the other.
• Union (∪): Consolidates tuples in two relations
4
1/21/2025
Operations
Few More Operations:
• Intersection (∩): Returns tuples present in both relations
• Join (⋈): Allows to combine two or more relations
• There are several types of joins:
• semi join, left outer join, right outer join, full outer join
• Division (/): Returns those tuples of a relation which
subsets tuples of another table
• Assignment (←): Assigning the output to a relation
5
1/21/2025
6
1/21/2025
7
1/21/2025
SN ID CourseID ID
1 101 CS101 101
2 102 CS102 102
3 103 MT101 103
4 102 EE101 104
5 104 CH102 105
6 105 CH101
8
1/21/2025
9
1/21/2025
Rename (ρ)
Renaming relation name and attributes:
• ρ<OUTPUT_RELATION>(<OUTPUT_ATTRIBUTES>)(<INPUT_RELATION>)
Renaming relation name only (and not attributes):
• ρ<OUTPUT_RELATION>(<INPUT_RELATION>)
Renaming attributes only (and not relation name):
• ρ(<OUTPUT_ATTRIBUTES>)(<INPUT_RELATION>)
The input for rename operator can be a relation or a relational
algebra expression
Rename operation is a special type of projection
10
1/21/2025
Set Operations
The set operations Union (∪), Intersection (∩), Difference
(−) are binary Operations.
Union (∪): Consolidates tuples in two relations
Intersection (∩): Extracts only common tuples from two
relations
Difference (−): Extracts tuples present exclusively in first
relation (but not present in second relation).
Set Operations
Union Compatibility: The two input relations should have
(i) same degree and (ii) same domain for every attribute
Union Compatibility is decided based on intrinsic properties
of two relations, not based on it’s state.
R ∪ S automatically stores the result in first operand, i.e.,
R.
Union eliminates duplicates from the output and returns the
common tuples only once in output, and intersection cannot
have duplicates.
Intersection is a derived operation, i.e.
R ∩ S = R − (R − S) = S − (S − R)
11
1/21/2025
Set Operations
Union and intersection operations are commutative, i.e.
• R ∪ S = S ∪ R and R ∩ S = S ∩ R
Union and intersection operations are associative, i.e.
• (R ∪ S) ∪ T = R ∪ (S ∪ T) and (R ∩ S) ∩ T = R ∩ (S ∩ T)
Difference is not commutative, i.e.,
• R − S != S − R
12
1/21/2025
Union ( ): Example
Students1 ∪ Students2
Union ( ): Example 2
πID (Students1) ∪ πID (Students2)
ID
101
102
103
13
1/21/2025
14
1/21/2025
No Output
15
1/21/2025
Join ( )
• Combines related tuple from two tables based on related column.
• Syntax: P ← R⋈<join condition>S
• Join product is commutative, i.e, R⋈<join condition>S = S⋈<join condition>R
• Arity of R ⋈ S = (Arity of R + Arity of S)
• Minimum Cardinality of R ⋈ S = 0
• Maximum Cardinality of R⋈S=(Cardinality of R × Cardinality of S)
• <join condition> is denoted as Ri θ Sj where Ri and Sj are two
attributes from the relations respectively, and θ is the binary
Boolean operation between them.
• θ can be checking if they are equal, or unequal, or one is greater
than other etc.
• As the operation between attributes from different relations is
classically represented by θ, it is also called theta join.
16
1/21/2025
Join ( )
Types:
• Natural Join: Matches rows with the same value in common
columns.
• Theta Join: Combines rows based on a condition.
• Outer Join: Includes unmatched rows (left, right, or full outer
joins).
Natural Join ( or *)
A special type of join operation, where <join condition> is not
explicitly written.
An equality check between the attribute pairs having same name is
done, and accordingly the result of natural join is computed.
R ⋈ S or R * S
A B C A
A1 B1 C1 A1
A2 B2 C2
* A2
A3 B3 C2
A B C
A1 B1 C1
A2 B2 C2
17
1/21/2025
18
1/21/2025
Theta Join ( θ)
Combines rows based on a condition (θ).
Students⋈(Students.StudentID = Enrollments.StudentID) Enrollments
19
1/21/2025
20
1/21/2025
Inner Join
Combines rows from two tables where there is a match based
on a specified condition. It is a specific case of theta join
where the condition is equality.
Students⋈(Students.StudentID = Enrollments.StudentID) Enrollments
Outer Join
Combines rows from two tables, including unmatched rows,
by filling in NULL values where matches are not found.
Types of outer joins:
• Left Outer Join
• Right Outer Join
• Full Outer Join
21
1/21/2025
22
1/21/2025
Semi Join ( )
A semi-join between two relations returns tuples from the
one relation where one or more matches (according to join
condition) are found in tuples of the other relation.
Though a tuple in one relation may have match with more
than one tuples of the other relation, the tuple will appear
only once in output.
left semi-join (R ⋉ S) retains those tuples of R which have a
match with S.
right semi-join (R ⋊ S) retains those tuples of S which have a
match with R
Semi-join is not commutative.
23
1/21/2025
StudentID Course ID
1 CS101
3 CS102
24
1/21/2025
Anti Join
A anti-join or anti semi-join between two relations returns
those tuples from the one relation for which there exists no
corresponding matches (according to join condition) in tuples
of the other relation.
left anti-join or called ONLY anti-join (R ▷ S) retains those
tuples of R which do not have a match with S
• R▷S = R - (R ⋉ S)
right anti-join (R ◁ S) retains those tuples of S which do not
have a match with R.
• R◁ S = S - (R ⋊ S)
anti-join is not commutative.
25
1/21/2025
StudentID CourseID
4 CS103
26
1/21/2025
Division (/)
P ← R / S will retain those attributes which are present in R
but not in S only for those tuples where ALL values of
common attributes in R matches any tuple of S
Syntax: P R/S
Note: P*S may not return R, Non-Commutative.
Find students taken all required courses.
Name CourseID
CourseID
Vineet CS101
CS101
Vineet CS102 / CS102
Surya CS103
Ashish CS102
Name
Vineet
Department of CSE, National Institute of Technology Rourkela
Division (/)
P ← R / S will retain those attributes which are present in R
but not in S only for those tuples where ALL values of
common attributes in R matches any tuple of S
Syntax: P R/S
Find students taken all required courses.
Name CourseID
CourseID
Vineet CS101
CS101
Vineet CS102 / CS102
Surya CS103
Ashish CS102
Ashish CS101 Name
Vineet
Ashish
27
1/21/2025
Division (/)
T←R/S
• T1 ← π(R-S)(R)
• T2 ← π(R-S) ((S × T1) - R)
• T ← T1 - T2
Relational Calculus
28
1/21/2025
Relational Calculus
Relational calculus is a non-procedural query language
used in relational databases to describe the desired
results of a query without specifying how to compute
them.
It is based on mathematical logic and works with
variables, predicates, and logical connectives to define
queries.
Relational Calculus
Relational Calculus
TRC and DRC are equivalent in power.
TRC and DRC are also equivalent in power with
relational algebra.
29
1/21/2025
30
1/21/2025
31
1/21/2025
Name
Vineet
32
1/21/2025
GATE Questions
Gate Questions
33
1/21/2025
Gate Questions
Gate Questions
• R – R(R-S):
• R-S: computes the set of tuples in R but not in S
• R-(R-S), removes from R all tuples that are not in S, leaving only the
tuples common to both R and S. (reason for minimality: because it uses
only (-) operator.
• Other non minimal solutions:
– R∩S=(R∪S)−((R−S)∪(S−R))
– (R−(R−S))−(S−R)
34
1/21/2025
Gate Questions
Gate Questions
• First Part:
– Perform Cartesian product PxR
– Select P.Y = R.Y AND R.V = V
– Project attribute X from result X1, X2
• Second Term:
– Perform QxR.
– Select Q.Y = R.Y AND Q.T>2
– Project attribute X from result X2
• At last compute difference: X retrieved in first term – X retrieved in second
term. Result X1
35