Dbms Relational Algebra
Dbms Relational Algebra
Syntax: R1 ⋈θ R2
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 = Φ. Here, the θ is of the form R1.a θ R2.b, and θ is any of the
comparison operators {=, <, <=, >, >=, ≠}. It produces all combinations
of tuples from R1 and R2 that satisfy the join condition. This join
condition involves attributes from both relations such as follows;
R1.a θ R2.b
If R1(A1, A2, …, An) is joined with R2(B1, B2, …, Bn) then it produces
a new temporary relation R(A1, A2, …, An, B1, B2, …, Bn) and every
tuple (record) of R1 is combined with every tuple of R2. The result
consists of all records that satisfy the join condition.
Example:
Table 1:
Table A
Column 1 Column 2
1 1
1 2
Table 2:
Table B
Column 1 Column 2
1 1
1 3
b) Equi Join:
Syntax: R1 ⋈ R1.a=R2.a R2
Example:
Table 1:
Patient P
Age Zip Disease
54 98125 Heart
20 98120 flu
Table 2:
Voters V
Name Age Zip
Rajesh 54 98125
Ramesh 20 98120
P ⋈P.age = V.age V
P ⋈P.age = V.age V
c) Natural Join:
Natural join does not use any comparison operator. It does not concatenate the
way a Cartesian product does. We can perform a Natural Join only if there is at
least one common attribute that exists between two relations. In addition, the
attributes must have the same name and domain.
Natural join acts on those matching attributes where the values of attributes in
both the relations are same.
Same as EQUIJOIN except that the join attributes of R2 are not included in the
resulting relation; if the join attributes have the same names, they do not have to
be specified at all.
If R1(A, B, C) is joined with R2(A, D, E), then it produces a new temporary
relation R(A, B, C, D, E). The result consists of all records that satisfy the join
condition. Also, observe that R does not include A twice.
Natural join is just like equi-join. Only difference is that the common attribute is
not included in the result twice in natural join unlike equi-join.
Syntax: Table 1 ⋈ Table 2
Example:
Table 1:
Courses
CID Course Dept
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
Table 2:
HoD
Dept Head
CS Alex
ME Maya
EE Mira
Using natural join:
Courses ⋈ HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Maya
EE EE01 Electronics Mira
d) Outer Join:
It is used when we want to keep all the tuples in either or both the
relations in the result of the JOIN, regardless of whether or not they have
matching tuples in other relation.
All the tuples from the Left relation, R, are included in the
resulting relation. If there are tuples in R without any matching
tuple in the Right relation S, then the S-attributes of the resulting
relation are made NULL.
Example:
Table 1:
Left
A B
100 Database
101 Mechanics
102 Electronics
Table 2:
Right
A B
100 Alex
102 Maya
104 Mira
All the tuples from the Right relation, S, are included in the
resulting relation. If there are tuples in S without any matching
tuple in R, then the R-attributes of resulting relation are made
NULL.
Courses HoD
A B C D
100 Database 100 Alex
102 Electronics 102 Maya
--- --- 104 Mira
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.
Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
--- --- 104 Mira
------------------------------------------------------------------------------------------------
Example:
List all staff with a salary greater than £10,000 from the given
table.
ii) Projection:
Example:
ΠID, Name(STUDENT)
ID Name
100 Ashish
200 Rahul
300 Naina
400 Sameer
Example:
Table 1: STUDENT
Table 2: DETAIL
ROLLNO AGE
2 18
6 21
iv) Union
a. R ∪ S
– Union of two relations R and S defines a relation that contains
all the tuples of R, or S, or both R and S, duplicate tuples being
eliminated.
Example:
Table A
Column 1 Column 2
1 1
1 2
Table B
Column 1 Column 2
1 1
1 3
AUB:
Column 1 Column 2
1 1
1 2
1 3
v) Intersection:
Lets say we have two relations R1 and R2 both have same columns
and we want to select all those tuples(rows) that are present in both
the relations, then in that case we can apply intersection operation
on these two relations R1 ∩ R2.
Example:
Table A
Column 1 Column 2
1 1
1 2
Table B
Column 1 Column 2
1 1
1 3
A∩B:
Column 1 Column 2
1 1
2. Explain Relational Algebra using the operators {σ, Π, U,-,X and show
that: A∩B = AUB – ((A-B)U(B-A))
It is denoted by σ.
Code:
ii. The project operation: returns its argument relation with certain
attributes left out.
It is denoted as Π.
Example: List all the title and ace-no of the “Book” relation.
iii. The union operation: It is used when we need some attributes that
appear in either or both of the two relations.
It is denoted as U.
Example:
The relation r and s must be of the same arity, i.e. they must have
the same number of attributes.
The domains of the ith attribute of r and the ith attribute of s must
be the same for all i.
iv. The set difference operation: It finds tuples in one relation but not
in other.
It is denoted as ‘-’
Example:
Find the names of all customers who have an account but not a
loan.
Code:
A B
a 1
b 2
c 2
Code:
B C
3 1a
2 2b
Therefore, r X s gives:
Code:
From the Venn diagram, the intersection of the two sets A&B in
the shaded part i.e., C:
= (A U B) – ((A-B) U (B-A)) = C
3. Let R = (A, B) and S = (A, C), and let r(R) and s(S) be relations. Write
relational-algebra expressions equivalent to the following domain-
relational calculus expressions:
a. {< a > | ∃ b (< a, b > belongs to r ∧ b = 17)}
b. {< a, b, c > | < a, b > ∈ r ∧ < a, c > belongs to s}
Ans.
a. ΠA (σB = 17 (r))
b. r⋈s
a) Find the names of all employees who work for First Bank Corporation.
Ans. Πperson-name (σcompany-name = “First Bank Corporation” )
b) Find the names and cities of residence of all employees who work for
First Bank Corporation.
Ans. Πperson-name, city (employee ⋈ (σcompany-name = “First Bank Corporation” (works))
c) Find the names, street address, and cities of residence of all employees
who work for First Bank Corporation and earn more than $10,000 per
annum.
Ans. Πperson-name, street, city (σ(company-name = “First Bank Corporation” ∧ salary > 10000) works
⋈ employee)
d) Find the names of all employees in this database who live in the same
city
as the company for which they work.
Ans. Πperson-name (employee ⋈ works ⋈ company)
e) Find the names of all employees who live in the same city and on the
same street as do their managers.
Ans. Πperson-name ((employee ⋈ manages) ⋈ (manager-name = employee2.person-name ∧
employee.street = employee2.street ∧ employee.city = employee2.city)(ρemployee2 (employee)))
f) Find the names of all employees in this database who do not work for
First Bank Corporation.
Ans. Πperson-name (σcompany-name = “First Bank Corporation” (works))
g) Find the names of all employees who earn more than every employee of
Small Bank Corporation.
Ans. Πperson-name (works) − (Πworks.person-name (works ⋈ (works.salary ≤works2.salary ∧
works2.company-name = “Small Bank Corporation”) ρworks2(works)))
5. Give expressions using the tuple relational calculus and the domain
relational calculus to express each of the following queries:
a) Find the names of all employees who work for First Bank Corporation.
Ans.
i. {t | ∃ s ∈ works (t[person-name] = s[person-name] ∧ s[company-
name] = “First Bank Corporation”)}
ii. {< p > | ∃ c, s (< p, c, s > ∈ works ∧ c = “First Bank
Corporation”)}
b) Find the names and cities of residence of all employees who work for
First Bank Corporation.
Ans.
i. {t | ∃ r ∈ employee ∃ s ∈ works ( t[person-name] = r[person-name] ∧
t[city] = r[city] ∧ r[person-name] = s[person-name] ∧ s[company-
name] = “First Bank Corporation”)}
ii. {< p, c > | ∃ co, sa, st (< p, co, sa > ∈ works ∧ < p, st, c > ∈ employee
∧ co = “First Bank Corporation”)
c) Find the names, street address, and cities of residence of all employees
who work for First Bank Corporation and earn more than $10,000 per
annum.
Ans.
i. {t | t ∈ employee ∧ (∃ s ∈ works ( s[person-name] = t[person-name] ∧
s[company-name] = “First Bank Corporation” ∧ s[salary] > 10000))}
ii. {< p, s, c > | < p, s, c > ∈ employee ∧ ∃ co, sa (< p, co, sa > ∈ works ∧
co = “First Bank Corporation” ∧ sa > 10000)
d) Find the names of all employees in this database who live in the same
city as the company for which they work.
Ans.
i. {t | ∃ e ∈ employee ∃ w ∈ works ∃ c ∈ company (t[person-name] =
e[person-name] ∧ e[person-name] = w[person-name] ∧ w[company-
name] = c[company-name] ∧ e[city] = c[city])
ii. {< p > | ∃ st, c, co, sa (< p, st, c > ∈ employee ∧ < p, co, sa > ∈ works
∧ < co, c > ∈ company)}
e) Find the names of all employees who live in the same city and on the
same street as do their managers.
Ans.
i. { t | ∃ l ∈ employee ∃ m ∈ manages ∃ r ∈ employee (l[person-name] =
m[person-name] ∧ m[manager-name] = r[person-name] ∧ l[street] =
r[street] ∧ l[city] = r[city] ∧ t[person-name] = l[person-name])}
ii. {< t > | ∃ s, c, m (< t, s, c > ∈ employee ∧ < t, m > ∈ manages ∧ < m,
s, c > ∈ employee)}
f) Find the names of all employees in this database who do not work for
First Bank Corporation.
Ans.
i. { t | ∃ w ∈ works ( w[company-name] ≠ “First Bank Corporation” ∧
t[person-name] = w[person-name])}
ii. { < p > | ∃ c, s (< p, c, s > ∈ works ∧ c ≠ “First Bank Corporation”)}
g) Find the names of all employees who earn more than every employee of
Small Bank Corporation.
Ans.
i. {t | ∃ w ∈ works (t[person-name] = w[person-name] ∧ ∀ s ∈ works
(s[company-name] = “Small Bank Corporation” ⇒ w[salary] >
s[salary]))}
ii. {< p > | ∃ c, s (< p, c, s > ∈ works ∧ ∀ p2, c2, s2 (< p2, c2, s2 > ∉
works ∨ c2 ≠ “Small Bank Corporation” ∨ s > s2))}