0% found this document useful (0 votes)
64 views13 pages

Difference Between The Following

Uploaded by

Ritesh Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views13 pages

Difference Between The Following

Uploaded by

Ritesh Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Difference between the following:


a) Theta Join
 Theta Join allows you to merge two tables based on the condition
represented by theta. Theta joins works for all comparison operators.
It is denoted by symbol θ. The general case of JOIN operation is called
a Theta Join.

Syntax: - A ⋈θ B
 Theta join can use any condition in the selection criteria
Consider the following tables

Table A Table B

Column 1 Column 2 Column 1 Column 2

1 1 1 1

1 2 1 3

 For example:

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
 EQUI JOIN is done when a Theta join uses only the equivalence condition.
EQUI join is the most difficult operation to implement efficiently in an
RDBMS, and one reason why RDBMS have essential performance
problems.
 For example:
A⋈ A. column 2 = B.column 2 (B)

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

Column 1 Column 2

1 2

c) NATURAL JOIN
 NATURAL JOIN does not utilize any of the comparison operators. In
this type of join, the attributes should have the same name and
domain. In Natural Join, there should be at least one common
attribute between two relations.
It performs selection forming equality on those attributes which
appear in both relations and eliminates the duplicate attributes.
 For example: -

Table C Table D

Num square Num Cube

2 4 2 8

3 9 3 18

C⋈D

C⋈D
Num Square Cube

2 4 8
3 9 18

d) Outer JOIN
 An OUTER JOIN doesn’t require each record in the two join tables to
have a matching record. In this type of join, the table retains each
record even if no other matching record exists.
 There are three types of Outer joins : -
 Left Outer join
 Right Outer join
 Full Outer join

Define the five basic operators of relational algebra with an example each.

I. SELECT(σ)
 The SELECT operation is used for selecting a subset of the tuples according to a
given selection condition. It denotes sigma(σ) symbol. It is used as an
expression to choose tuples which meet the selection condition. Select operator
selects tuples that satisfy a given predicate.

σp(r) where ,
 σ is the predicate
 ‘r’ stands for relation which is the name of the table
 P is prepositional logic.
 Example -

σtopic=”Database” (Tutorials)

Output –Selects tuples from Tutorials where topic=’Database’.

II. PROJECTION(∏)
 The projection eliminates all attributes of the input relation but those
mentioned in the projection list. The projection method defines that contains a
vertical subset of Relation.
 This helps to extract the value of specified attributes to eliminate duplicate
values. (Pi) symbol is used to choose attribute from a relation. This operator
helps you to keep specific columns from a relation and discards the other
columns.
 Example of projection :

Customerid CustomerNAme Status


1 Google Active
2 Amazon Active
3 Apple Inactive
4 Alibaba Active

Here, the projection of CustomerName and status will give


∏CustomerName, Status (Customers)
Output –

CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active

III. UNION OPERATION(Ս)


 UNION is symbolized by Ս symbol. It includes all tuples that are in tables
A or in B. it also eliminates duplicate tuples. So, set A UNION set B would
be expressed as: A Ս B

 For a union operation to be valid, the following conditions must hold-

 Attribute domain need to be compatible


 Duplicate tuples should be automatically removed.
 Example :-
Consider the following tables.

Table A Table B
Column 1 Column 2 Column 1 Column 2
1 1 1 1
1 2 1 3

AՍB

OUTPUT -

Table A Ս B

Column 1 Column 2
1 1
1 2
1 3

IV. SET DIFFERNCE(-)


 It denotes (-) symbol. The result of A-B, is a relation which include all
tuples that are in A but not in B.
 The attribute name of A has to match with the attribute name in B.
 The two-operand relations A and B should be either compatible or
Union compatible.
 It should be defined relation consisting of the tuples that are in
relation A, but not in B.
 Example :

A-B

Table A Table B

Column 1 Column 2 Column 1 Column 2


1 1 1 1
1 2 1 3
OUTPUT -

Table A – B
Column 1 Column 2
1 2

V. INTERSECTION
 An intersection is defined by the symbol ∩.
 It defines a relation consisting of a set of all tuple that are in both A and
B. However, A and B must be Union-compatible.
 Example –

A∩B

Table A Table B

Column 1 Column 2 Column 1 Column 2


1 1 1 1
1 2 1 3

OUTPUT-

Table A ∩ B
Column 1 Column 2
1 1

2. Explain Relational Algebra using the operators{∂ ,∏,∪,-,X and show that :
A∩B-((A-B)∪(B-A))
First part:
 The select operation is to identify a set of tuples which is a part
of a relation and to extract only these 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 σ.

Consider the following table”Book”:

Ace-no Yr-pub Title


734216 1982 Algorithm design
237325 1995 Database systems
631523 1992 Compiler design
543211 1991 Programming
376112 1992 Machine design

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 of the 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.

Consider the following relation or table “r”:

A B
a 1
b 2
c 2

Consider another relation or table “s”.

B C
3 1a
2 2b

Therefore r Χ s gives:

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


a 1 3 1a
a 1 2 2b
b 2 3 1a
b 2 2 2b
B 2 3 1a
c 2 2 2b

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
3. Let R=(A,B) and S=(A,C) and let r® and r(S) be relations. Write relational
algebra expression equivalent to the domain relational calculus
expressions:
a) {<a>| there exist b(<a,b> belongs to r^b=7}
 ∏˄(σB=7(r))
b) {<a,b,c> |<a,b. belongs to r^<a,c> belongs to s}
 r1s

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.
 ∏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.city) (ρ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=’Small Bank 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))∏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.
 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[company-
name]=”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-
 {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”)}
 Domain relational calculus-
 {<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.
 Tuple relation calculus-
 {t|t Є employee ˄ (Ǝ s Є works(s[person-name]=t[person-name] ˄
s[company-name]=”First Bank Corporation” ˄ s[salary]>10000))}
 Domain relational calculus-
 {<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[person-
name] ˄ e[person-name]=w[person-name] ˄ w[company-
name]=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[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)}
 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[person-
name]=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.
 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)}

You might also like