Professional Documents
Culture Documents
NIT Rourkela
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 1 / 47
Outline
3 Relational Algebra
4 Relational Calculus
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 2 / 47
Introduction to Query Languages
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 3 / 47
Introduction to Query Languages
Relational Algebra and Relational Calculus (with only safe queries) are equivalent.
In practice, Relational model is implemented as RDBMS, and the query model is implemented
as SQL.
A language is called relationally complete if it has capacity to express every query expressible
through relational algebra. SQL is relationally complete.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 4 / 47
Introduction to Query Languages
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 5 / 47
Relational Algebra
Basic Operations
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
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 6 / 47
Relational Algebra
More Operations
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 7 / 47
Relational Algebra
Selection operation
Selection (σ): selects a subset of tuples / rows from a relation.
Also called as horizontal partitioning.
Syntax: σ<CONDITION> (<RELATION>)
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 8 / 47
Relational Algebra
Selection operation
Selection (σ): selects a subset of tuples / rows from a relation.
Also called as horizontal partitioning.
Syntax: σ<CONDITION> (<RELATION>)
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 9 / 47
Relational Algebra
Selection operation
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 10 / 47
Relational Algebra
Projection operation
Projection (π): selects a subset of attributes from a relation.
Also called as vertical partitioning.
Syntax: π<ATTRIBUTES> (<RELATION>)
Removes duplicates!
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 11 / 47
Relational Algebra
Projection operation
We know, for a projection operation to work, the attributes it retrieves must be a subset of the
attributes of the input relation.
For π<ATTRIBUTES2 > (π<ATTRIBUTES1 > (<RELATION>)) to be valid, the following condition must be true:
<ATTRIBUTES2 > ⊆ <ATTRIBUTES1 >
Also, for projection to be commutative,
π<ATTRIBUTES2 > (π<ATTRIBUTES1 > (<RELATION>)) = π<ATTRIBUTES1 > (π<ATTRIBUTES2 > (<RELATION>))
However, π<ATTRIBUTES2 > (π<ATTRIBUTES1 > (<RELATION>)) = π<ATTRIBUTES2 > (<RELATION>)
Hence projection is not commutative.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 12 / 47
Relational Algebra
Projection operation
Projection operator can act on a relation or any relational algebra expression resulting on
a table
The <ATTRIBUTES> involved in selection operation MUST be a subset (may be equal) of
all attributes in <RELATION>
Projection operator may reduce the arity / degree of a relation when it acts on,
i.e., Arity of π<ATTRIBUTES> (<RELATION>) ≤ Arity of <RELATION>
Projection operator may alter the cardinality of a relation as it removes duplicate tuples
from output
i.e., |π<ATTRIBUTES> (<RELATION>)| ≤ |<RELATION>|
If <ATTRIBUTES> is a superkey, then
|π<ATTRIBUTES> (<RELATION>)| = |<RELATION>|
Projection operation defined in abstract relational algebra language eliminates duplicates,
but projection operator in SQL retains duplicates!
Projection operation is not commutative.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 13 / 47
Relational Algebra
Please note that πEID,ENAME (σESAL > 10000 (EMP)) 6= σESAL > 10000 (πEID,ENAME (EMP)),
i.e., the roles of selection and projection cannot be commutative. Always selection acts
first, and then projection.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 14 / 47
Relational Algebra
Rename operation
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 15 / 47
Relational Algebra
Set operations
The set operations Union (∪), Intersection (∩), Difference (−) are binary Operations.
Difference (−): Extracts tuples present exclusively in first relation (but not present in second
relation)
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 16 / 47
Relational Algebra
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.
Union eliminates duplicates from the output and returns the common tuples only once in
output, and intersection cannot have duplicates.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 17 / 47
Relational Algebra
Set operations
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 18 / 47
Relational Algebra
A−B 0 m
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 19 / 47
Relational Algebra
The input relations need not be union compatible for participating in cross product.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 20 / 47
Relational Algebra
R(A, B, C)
P(A, B, C, D, E) ← R(A, B, C) × S(D, E)
A B C
a1 b1 c1 A B C D E
a2 b2 c2 a1 b1 c1 d1 e1
a3 b3 c3 a1 b1 c1 d2 e2
a2 b2 c2 d1 e1
S(D, E)
a2 b2 c2 d2 e2
D E a3 b3 c3 d1 e1
d1 e1 a3 b3 c3 d2 e2
d2 e2
Cross product is commutative, i.e, R × S = S × R
Cross product is associative, i.e., (R × S) × T = R × (S × T)
In most practical cases, such unconditional linking of every tuple of a relation with every
tuple of another relation is meaningless.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 21 / 47
Relational Algebra
Join
R(A, B, C)
A B C
a1 b1 c1
P(A, B, C, D, E) ← R(A, B, C) ./A = D S(D, E)
a2 b2 c2
a3 b3 c3 A B C D E
a1 b1 c1 a1 b1
S(D, E)
a2 b2 c2 a2 b2
D E
a1 b1
a2 b2
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)
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 22 / 47
Relational Algebra
Join
<join condition> is usually a condition where check between attributes of both participating
relations is performed.
<join condition> is hence 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.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 23 / 47
Relational Algebra
Join
<join condition> is very common in some practical cases, as we may check relation bewteen
two related attributes.
For this, a special type of join operation, called natural join (∗) is used 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(A, B, C)
A B C
a1 b1 c1
P(A, B, C) ← R(A, B, C) ∗ S(A)
a2 b2 c2
a3 b3 c3 A B C
a1 b1 c1
S(A)
a2 b2 c2
A
a1
a2
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 24 / 47
Relational Algebra
Join
R(A, B, C)
A B C
a1 b1 c1
a1 b2 c1
P(A, B, C, D) ← R(A, B, C) ∗ S(A, B, D)
a2 b1 c1
a2 b2 c2 A B C D
a3 b3 c3 a1 b1 c1 d1
a2 b2 c2 d2
S(A, B, D)
A B D
a1 b1 d1
a2 b2 d2
If no common attribute is there for natural join, it will degrade to cross product.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 25 / 47
Relational Algebra
Division
Syntax: P ← R / S
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
R(A, B)
A B
a1 b1
a2 b1 P(B) ← R(A, B) / S(A)
a1 b2
B
S(A) b1
A
a1
a2
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 26 / 47
Relational Algebra
Division
R(A, B)
A B
a1 b1
a2 b1
a3 b1 S(A)
P(B) ← R(A, B) / S(A)
a4 b1
A
a1 b2 B
a1
a3 b2 b1
a2
a2 b3 b4
a3
a3 b3
a4 b3
a1 b4
a2 b4
a3 b4
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 27 / 47
Relational Algebra
Division
R(A, B, C)
A B C
a1 b1 c1
a2 b1 c1
a3 b1 c3 S(A, C)
a4 b1 c2 P(B) ← R(A, B, C) / S(A, C)
A C
a1 b2 c1
a1 c1 B
a3 b2 c1
a2 c1 b1
a2 b3 c1
a3 c3
a3 b3 c1
a4 b3 c1
a1 b4 c2
a2 b4 c2
a3 b4 c3
Is division associative? Draw a Venn Diagram of set of three relations and check if R/(S/T) and
(R/S)/T contains same set of attributes.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 28 / 47
Relational Algebra
Division
Division is not a basic operation, that means, division operation should be expressed in terms of
basic operations.
Step# 3: T ← T1 - T2
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 29 / 47
Relational Algebra
Outer Join
All join functions learn before are inner join, in which the dangling tuples (those tuples of one
relation not having a referred value in other relation) does not appear in output.
Sometimes we may need those tuples which does not have a match in other relation.
left outer join ( R ./ S) retains the dangling tuples of R but not those of S
right outer join ( R ./ S) retains the dangling tuples of S but not those of R
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 30 / 47
Relational Algebra
Outer join
R ./A=B S
A C B D
1 3 1 6
R(A, C)
2 4 NULL NULL
A C
R ./ A=B S
1 3
2 4 A C B D
1 3 1 6
S(B, D)
NULL NULL 5 7
B D
R ./ A=B S
1 6
5 7 A C B D
1 3 1 6
2 4 NULL NULL
NULL NULL 5 7
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 31 / 47
Relational Algebra
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 32 / 47
Relational Algebra
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.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 33 / 47
Relational Algebra
Semi-join
R(A, B, C)
A B C P(A, B, C) ← R(A, B, C) nA=D S(D, E, F)
a1 b1 c1
A B C
a1 b2 c1
a1 b1 c1
a2 b1 c1
a1 b2 c1
a3 b2 c2
a2 b1 c1
a4 b3 c3
Q(D, E, F) ← R(A, B, C) oA=D S(D, E, F)
S(D, E, F)
D E F
D E F
a1 e1 f1
a1 e1 f1
a1 e2 f3
a1 e2 f3
a2 e2 f2
a2 e2 f2
a5 e2 f3
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 34 / 47
Relational Algebra
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 n S)
right anti-join (R / S) retains those tuples of S which do not have a match with R
R / S = S - (R o S)
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 35 / 47
Relational Algebra
Anti-join
R(A, B, C)
A B C
a1 b1 c1 P(A, B, C) ← R(A, B, C) .A=D S(D, E, F)
a1 b2 c1
a2 b1 c1 A B C
a3 b2 c2 a3 b2 c2
a4 b3 c3 a4 b3 c3
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 36 / 47
Relational Algebra
Inputs: A having A ./ B 0 mn
cardinality m, B having
cardinality n AnB 0 m
A ./ B n mn
A ./ B max(m,n) mn
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 37 / 47
Relational Algebra
Q. Which of the following query transformations (i.e. replacing the l.h.s. expression by the r.h.s.
expression) are incorrect?
R1 and R2 are relations, C1 and C2 are selection conditions, and A1 and A2 are attributes of R1 .
(a) σC1 (σC2 (R1 )) → σC2 (σC2 (R1 ))
(b) σC1 (πA1 (R1 )) → πA1 (σC1 (R1 ))
(c) σC1 (R1 ∪ R2 ) → σC1 (R1 ) ∪ σC1 (R2 )
(d) πA2 (σC1 (R1 )) → σC1 (πA2 (R1 ))
[GATE1998]
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 38 / 47
Relational Algebra
Q. Suppose R1 (A,B) and R2 (C,D) are two relation schemas. Let r1 and r2 be the corresponding
relation instances. B is a foreign key that refers to C in R2 . If the data in r1 and r2 satisfy
referential integrity constraints, which of the following is ALWAYS TRUE?
(a) πB (r1 ) − πC (r2 ) = φ
(b) πC (r2 ) − πB (r1 ) = φ
(c) πB (r1 ) = πC (r2 )
(d) πB (r1 ) − πC (r2 ) 6= φ
[GATE2012]
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 39 / 47
Relational Algebra
Q. Give a relational algebra expression using only the minimum number of operators from {∪ ,
- } which is equivalent to R ∩ S.
[GATE1994]
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 40 / 47
Relational Algebra
Q. Consider a join of a relation R with a relation S. If R has m tuples and S has n tuples, then the
maximum and minimum sizes of the join respectively are:
(a) m+n and 0
(b) mn and 0
(c) m+n and |m-n|
(d) mn and m+n
[GATE1999]
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 41 / 47
Relational Calculus
TRC and DRC are equivalent in power (when only safe queries are concerned), i.e., any query
expressed through TRC do have a parallel query in DRC that serve the same purpose.
TRC and DRC are also equivalent in power with relational algebra.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 42 / 47
Tuple Relational Calculus
P(A,B,C)
A B C Find those values of As for which C has a
a1 b1 c1 value c1
a2 b1 c1
a3 b1 c3 {t.A | P(t) AND t.C = c1 }
a4 b1 c2
a1 b2 c1 A
a3 b2 c2 a1
a2 b3 c1 a2
a3 b3 c1 a3
a4 b3 c1 a4
a1 b4 c2
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 43 / 47
Tuple Relational Calculus
Composite expressions:
Atomic expressions:
Range relation:
Range relation:
R1 (t1 ) ∧ R2 (t2 ) ∧ . . . ∧ Rn (tn )
R(t)
Conditions:
Conditions:
(i) with free variables:
t.A θ constant
F1 ∧ F2 , F1 ∨ F2 , ¬ F1
t1 .A θ t2 .B
(ii) with bounded variables using quantifiers: ∀ t(F), ∃ t(F)
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 44 / 47
Tuple Relational Calculus
List the names and addresses of all employees who work for department named ‘CSE’. The
database schema is as following:
EMP(ENAME, EADDRESS, EDOB, ESAL, DNO)
DEPT(DNO, DNAME, DMGR)
EMP.DNO is foreign key to DEPT.DNO
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 45 / 47
Domain Relational Calculus
P(A,B,C)
A B C Find those values of As for which C has a
a1 b1 c1 value c1
a2 b1 c1
a3 b1 c3 {<a> | ∃b ∃c (P(a,b,c) AND c = c1 )}
a4 b1 c2
a1 b2 c1 A
a3 b2 c2 a1
a2 b3 c1 a2
a3 b3 c1 a3
a4 b3 c1 a4
a1 b4 c2
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 46 / 47
Domain Relational Calculus
We build up formulae from atoms by using the following recursively applied rules:
An atom is a formula.
If P is a formula, then so are ¬P and (P).
If P1 and P2 are formulae, then so are P1 ∧ P2, P1 ∨ P2, and P1 ⇒ P2
If P(x) is a formula in x, where x is a domain variable, then: ∃x(P(x)) and ∀x(P(x)) are
also formulae.
Dr. Sambit Bakshi (NIT Rourkela) Abstract Query Languages: February 18, 2019 47 / 47