DBMS ASSIGNMENT For PRACTICE
DBMS ASSIGNMENT For PRACTICE
DBMS ASSIGNMENT For PRACTICE
b) Equi Join
When theta join uses only equality comparison operator, it is said to be equijoin. The
example corresponds to equijoin.
Student_Detail −
STUDENT ⋈Student.Std = Subject.Class SUBJECT
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.
A⋈ B
d) Outer Joins
Theta Join, Equijoin and Natural Join called inner joins. An inner join includes only
those tuples with matching attributes and the rest are discarded in the resulting relation.
Therefore, we need to use outer joins to include all the tuples from the participating
relations in the resulting relation. There are three kinds of outer join – left outer join,
right outer join, and full outer join.
Output – Select tuples from books where subject is ‘database’ and ‘price’ 450 or those
books published after 2010.
The result of set difference operation is tuples, which are present in one relation but are
not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
5) Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations, and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorial'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorial.
Q2. Explain Relational Algebra using the operators (∂, ∏, U, - , X and show that: A∩B
= AUB-((A-B) U (B-A)).
1. First part:
The select operation is to identify a set of tuples which is a part of a
relation and to extract only this tuple 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 as σ.
Example 1:- Select from the relation “Book” all the books whose year of
publication is 1992.
σYr-pub=1992(Book)
Example 2:- Select from the relation “Book” all the books whose Ace-no is
greater than equal to 56782.
σAcc-no>=56782(Book)
The project Operation returns its argument relation with certain attributes left out.
It is unary operation defined on a single relation.
It is denoted as ∏.
Example:- List all the Title and Ace-no of the “Book” relation.
∏Ace-no,Title(Book)
The Union operation is used when we need some attributes that appear in either or
both two relations.
It is denoted as ∪.
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 codes:
∏customer-name (Borrower)∪ ∏ customer-name (Depositor)
For a Union operation r ∪ s to be valid, two conditions must hold:
The relation r ∪s must be same,i.e., they must have the same number of
attributes.
The domains of the ith attributes of r and the ith attribute of s must be the
same for all i.
The set difference operation 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.
∏customer-name (Depositor)-∏customer-name (Borrower)
The Cartesian product operation allows combining information from two relations.
It is denoted as r X s where r and s are relations.
A B
a 1
b 2
c 2
B C
3 1a
2 2b
Therefore r Χ s gives:
2. Second Part
From the Venn diagram,the intersection of the two sets A &B in the shaded
part i.e., C A ∪ B=((A-B)∪ (B-A))∪ C
(A ∪B)-((A-B)∪(B-A))=C
A∩B-(A∩B)-((A-B)∩(B-A))[proved]
A C B
Q3. Let R=(A,B) and S=(A,C) and let r(R) and r(S) be relations.
Write relational algebra expression equivalent to the domain relational calculus
expression:
a) {<a> | there exist b (<a, b> belongs to r^b = 17}
=> ∏˄(σB=7(r))
=> r1s
Give an example in the relational algebra to express each of the following Queries:
a. Find the names of all employees who work for First Bank Corporation.
∏person-name (σ company-name=’First Bank Corporation’ (works))
b. Find the names and cities of residence of all employees who work for First Bank
Corporation.
∏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.
∏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.
∏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.
∏person-name(employee⋈manages)⋈(manager-
name=employee2.person-
name˄employee.street=employee2.street˄employee.city=employee2.cit
y) (ρemployee2
(employee)))
f. Find the names of all employees in this database who do not work for First Bank
Corporation.
∏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.
∏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.
∏company-name(company÷(∏city(σcompany-name=’SmallBank
Corporation’(company))))
i. Find the company with the most employees.
t1← company-nameGcount-distinct person-name(works)
t2← maxnum-employees (ρ company-name, num-employees)
(t1))∏company-name
(ρt3(company-name, num-employees)(t1) ⋈ ρt4(num-employees) (t2))
j. Find the company with the smallest payroll.
t1← company-nameGdum salary (works)
t2← minpayroll (ρcompany-payroll(company-name, payroll)
(t1))∏companyname(ρ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.
t2←company-nameG avg salary(works)
t2←σcompany-name=”First Bank Corporation”(t1)
5. Give expression 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.
Tuple relation calculus-
{t|Ǝ s Є works(t[person-name]=s[person-name]˄ s[companyname]=”First
Bank Corporation”)}
Domain relational calculus-
{<p>|Ǝ c,s (<p,c,s> Є works ˄ c=”First Bank Cororation”)}
b. Find the names and cities of residence of all employees who work for First Bank
Corporation.
Tuple relation calculus-
I. sa{t|Ǝ r Є employee Ǝ s Є works (t[person-name]=r[person-name] ˄ t[city]=r[city]
˄ r[person-name]=s[person-name] ˄ s[companyname]=”First Bank
Corporation”)}
Domain relational calculus-
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 Ban Corporation and earn more than $10,000 per annum.
a. Tuple relation calculus-
i. {t|t Є employee ˄ (Ǝ s Є works(s[person-name]=t[person-name] ˄
s[company-name]=”First Bank Corporation” ˄ s[salary]>10000))}
b. Domain relational calculus-
i. {<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 the database who live in the same city as the
company for which they work.
Tuple relational calculus-
{t|Ǝ e Є employee Ǝ w Є works Ǝ c Є company(t[person-
name]=e[personname] ˄ e[person-name]=w[person-name] ˄
w[companyname]=c[company-name] ˄ e[city]=c[city])}
Domain relational calculus-
{<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.
Tuple relational calculus-
{t|Ǝ l Є employee Ǝ m Є manages Ǝ r Є employee(l[personname]=m[person-
name] ˄ m[manager-name]=r[person-name] ˄ l[street]=r[street] ˄
l[city]=r[city] ˄ t[person-name]=l[person-name)}
a. Domain relational calculus-
{<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.
Tuple relational calculus-
{t|Ǝ w Є works(w[company-name]≠ ‘First Bank Corporation’ ˄
t[personname]=w[person-name])}
Domain relational calculus-
{<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.
Tuple relational calculus-
{t|Ǝ w Є works(t[person-name]=w[person-name] ˄ ∀ s Є
works(s[company-name]= ‘Small Bank
Corporation’⟹w[salary]>s[salary]))}
Domain relational calculus-
{<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.
i. Tuple relational calculus-
{t|∀ s Є company(s[company-name]= ‘Small Bank Corporation’ ⇒ Ǝ r Є
company(t[company-name]=r[company-name] ˄ r[city]= s[city]))}
Domain relational calculus-
{<co>|∀ co2,ci2(<co2,ci2> Є company ˄ co2= ‘Small Bank Corporation’⇒
<co2,ci2> Є company)}