Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

NAME – SHRUTI

ROLL NO. – 31401219044


SEMESTER – 4TH
YEAR – 2019-2022
SUBJECT – BCAN -401
1. Difference between the following:
a) Theta Join
b) Equi Join
c) Natural Join
d) Outer Join
Define the five basic operators of relational algebra with an example each.
Ans.
a) Theta Join: THETA JOIN allows you 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: 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

Using theta join:

A ⋈ A.column 2 > B.column 2 (B)

A ⋈ A.column 2 > B.column 2 (B)


Column 1 Column 2
1 2

b) Equi Join:

When Theta join uses only equality comparison operator, it is said to be


equijoin. Equijoin is a special case of conditional join where only equality
condition holds between a pair of attributes. As values of two attributes will be
equal in result of equijoin, only one attribute will be appeared in result.

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

Using equi join to obtain details of customers with same age:

P ⋈P.age = V.age V

P ⋈P.age = V.age V

Age P.zip Disease Name V.Zip


54 98125 Heart Rajesh 98125
20 98120 flu Ramesh 98120

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.

Outer join is of 3 types:

i) Left outer join (R   S)

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

Using left outer join:


Courses   HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya

ii) Right Outer Join (R   S)

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

iii) 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.

Courses   HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
--- --- 104 Mira

------------------------------------------------------------------------------------------------

The 5 basic operations of relational algebra are:

i) Selection (or Restriction):

σpredicate (R) – The SELECT operation is used for selecting a subset


of the tuples according to a given selection condition.
Sigma(σ)Symbol denotes it. It is used as an expression to choose
tuples which meet the selection condition. Select operator selects
tuples that satisfy a given predicate.

Example:

List all staff with a salary greater than £10,000 from the given
table.

Staff no. Name Position Salary Branch No


101 Anny Manager 30000 B004
102 Susan Employee 12000 B005
105 Nilesh Assistant 9000 B007
103 John Assistant 14000 B003
104 David Manager 23000 B003
111 Alex Employee 5000 B004

σsalary > 10000 (Staff)

Staff no. Name Position Salary Branch No


101 Anny Manager 30000 B004
102 Susan Employee 12000 B005
103 John Assistant 14000 B003
104 David Manager 23000 B003

ii) Projection:

Πcol1, . . . , coln(R) – The projection eliminates all attributes of the input


relation but those mentioned in the projection list. The projection
method defines a relation that contains a vertical subset of
Relation.

This helps to extract the values of specified attributes to eliminates


duplicate values. (pi) symbol is used to choose attributes from a
relation. This operator helps you to keep specific columns from a
relation and discards the other columns.

Example:

Produce a list of ID, Name from the given table STUDENT

ID Name Subject Age


100 Ashish Maths 19
200 Rahul Science 20
300 Naina Physics 20
400 Sameer Chemistry 21

ΠID, Name(STUDENT)

ID Name
100 Ashish
200 Rahul
300 Naina
400 Sameer

iii) Cartesian Product:

R X S – Defines a relation that is the concatenation of every tuple


of relation R with every tuple of relation S.

Example:

Table 1: STUDENT

SNO FNAME LNAME


1 Albert Singh
2 Nora Fatehi

Table 2: DETAIL

ROLLNO AGE
2 18
6 21

Therefore, STUDENT X DETAIL

SNO FNAME LNAME ROLLNO AGE


1 Albert Singh 5 18
1 Albert Singh 9 21
2 Nora Fatehi 5 18
2 Nora Fatehi 9 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.

– R and S must be union-compatible.

b. If R and S have I and J tuples, respectively, union is obtained


by concatenating them into one relation with a maximum of
(I + J) tuples.

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:

Intersection operator is denoted by ∩ symbol and it is used to


select common rows (tuples) from two tables (relations).

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))

Ans. First part:

i. The select operation: to identify a set of tuples which is a part of a


relation and to extract only these tuples out. The select operation
selects tuples that satisfy a given predicate or condition.

 It is a unary operation defined on a single relation.

 It is denoted by σ.

Consider the following table “Book”:

Code:

Ace-no Yr-pub Title


734216 1982 Algorithm design
237235 1995 Database systems
631523 1992 Compiler design
543211 1991 Programming
376112 1992 Machine Design
Example: Select from the relation “Book” all the books whose year of
publication is 1992.

Ans. σ Yr-pub = 1992(Book)

ii. The project operation: returns its argument relation with certain
attributes left out.

 It is a unary operation defined on a single relation.

 It is denoted as Π.

Example: List all the title and ace-no of the “Book” relation.

Ans. Π Ace-no, Title(Book)

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:

Borrower (customer-name, loan-number)


Depositor (customer-name, account-number)
Customer (customer-name, street-number, customer-city)
List all the customers who have either an account or a loan or both
code.

Ans. Π customer-name (Borrower) U Π customer-name


(Depositor)

For a union operator r U s to be valid, two conditions must hold:

 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.

Ans. Π customer-name (Depositor) - Π customer-name (Borrower)

v. The cartesian product operation: It allows combining information


from two relations.

 It is denoted as R X S where R and S are relations.

Consider the following relation or table “r”:

Code:

A B
a 1
b 2
c 2

Consider the following relation or table “s”:

Code:

B C
3 1a
2 2b

Therefore, r X s gives:

Code:

r.A r.B s.B s.C


a 1 3 1a
a 1 2 2b
b 2 3 1a
b 2 2 2b
a 2 3 1a
a 2 2 2b
Second part:

From the Venn diagram, the intersection of the two sets A&B in
the shaded part i.e., C:

AUB = ((A-B) U (B-A)) U C

= (A U B) – ((A-B) U (B-A)) = C

= A∩B – (A∩B) – ((A-B) ∩ (B-A))

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

4. Consider the following relational schema:


employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)
manages (person-name, manager-name)

Give an expression in the relational algebra to express each of the following


queries:

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)))

h) Assume the companies may be located in several cities. Find all


companies located in every city in which Small Bank Corporation is
located.
Ans. Πcompany-name (company ÷ (Πcity (σcompany-name = “Small Bank Corporation”
(company))))

i) Find the company with the most employees.


Ans. Πcompany-name(ρt3(company-name,num-employees)(t1) ⋈ ρt4(num-employees)(t2)

j) Find the company with the smallest payroll.


Ans. Πcompany-name(ρt3(company-name,payroll)(t1) ⋈ ρt4(payroll)(t2))
k) Find those companies whose employees earn a higher salary, on average,
than the average salary at First Bank Corporation.
Ans. Πt3.company-name((ρt3(company-name,avg-salary)(t1)) ⋈ t3.avg-salary > first-bank.avg-salary (ρfirst-
bank(company-name,avg-salary)(t2))

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))}

h) Assume the companies may be located in several cities. Find all


companies located in every city in which Small Bank Corporation is
located.
Ans.
i. {t | ∀ s ∈ company (s[company-name] = “Small Bank Corporation”
⇒ ∃ r ∈ company (t[company-name] = r[company-name] ∧ r[city] =
s[city]))}
ii. {< co > | ∀ co2, ci2 (< co2, ci2 > ∉ company ∨ co2 ≠ “Small Bank
Corporation” ∨ < co, ci2 > ∈ company)}

You might also like