Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Relational Algebra

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

Query Language

Query Language
• A Language which is used to store and retrieve
data from database is known as query
language. For example – SQL
• There are two types of query language:
1. Procedural Query language
2. Non-procedural query language
Procedural Query language
• The user instructs the system to perform a
series of operations to produce the desired
results.
• Here users tells what data to be retrieved
from database and how to retrieve it.
• For example – Making a cup of tea. If you are
telling the step by step process like switch on
the stove, boil the water, add milk etc. then it
is a procedural language.
Non Procedural Query Language
• The user instructs the system to produce the
desired result without telling the step by step
process.
• Here users tells what data to be retrieved
from database but doesn’t tell how to retrieve
it.
• Example: if you are just telling someone to
make a cup of tea and not telling the process
Conceptual Query Language
• Theoretical mathematical system or query language, to store
and retrieve data is called as Conceptual Query Language.
• They are not the practical implementation.
Types:
– Conceptual Procedural Query Language. E.g., Relational
Algebra
– Conceptual non-procedural query language. E.g., Relational
calculus
• Relational algebra and calculus are the theoretical concepts
used on relational model.
• SQL is a practical implementation of relational algebra and
calculus.
Relational Algebra
Relational Algebra
• The relational algebra is a procedural query
language or formal query language.
• It consists of a set of operations that take one or
two relations as input and produce a new relation
as their result.
• The select, project, and rename operations are
called unary operations, because they operate on
one relation.
• The other operations operate on pairs of
relations and are therefore called binary
operations.
Operators
• Basic Operators
– Projection ( )
– Selection ( )
– Cross Product (X)
– Union ( U)
– Rename ( )
– Set Difference (-)
• Derived Operators
– Join ( )
– Intersection ()
– Division ( / )
Project Operation – selection of
columns (Attributes)
• Relation r:

• A,C (r)
No duplicate data
Project Operation – selection of
columns (Attributes)
• Relation Student:

Roll Name Age


1 A 20
2 B 21
3 A 19

• Retrieve the roll no and name from table student.


• roll,name (Student)
Roll Name
1 A
2 B
3 A
Select Operation – selection of rows
(tuples)
• Relation r

(A=B) ^( D > 5) (r)


Select Operation – selection of rows
(tuples)
• Relation Student:
Roll Name Age
1 A 20
2 B 21
3 A 19

• Select the name of the student whose Roll=‘2’.

• roll=‘2’ (Student) 2 B 21

• name (roll=‘2’ (Student)) B


Cartesian-product / Cross Product
• Relations r, s:

A B C C D E
1 2 3 3 4 5
2 1 4 2 1 2

• r x s: no of column =(no of column in r) + (no of column in s)


=3+3=6
no of rows = (no of rows in r) * (no of rows in s)
=2*2=4 A B C C D E
1st row r 1st row s
1 2 3 3 4 5
1st row r 2nd row s
1 2 3 2 1 2
2nd row r 1st row s
2 1 4 3 4 5
2nd row r 2nd row s
2 1 4 2 1 2
Cartesian-product
• Relations r, s:

• r x s:
Cartesian-product – naming issue
Relations r, s: B

r x s: r.B s.B
Union of two relations
• Relations r, s:

r  s:

No duplicate values
Union of two relations
• Relations student, employee:

Roll Name Emp.no Name


1 A 7 E
2 B 1 A
3 C

student Employee
The no. of columns in both the tables should be same
If Domain of attributes are same, then only union operation take place
student  employee:
Roll Name
1 A
2 B Column name should
be similar to the 1st
3 C
table (left hand side of
7 E the union operator)
Union of two relations
• Relations student, employee:

Roll Name Emp.no Name


1 A 7 E
2 B 1 A
3 C

student Employee
name (student)  name (employee): The person who are student or employee
or both

Name
A
B
C
E
Renaming a Table
• Allows us to refer to a relation, (say E) by more than
one name.
 x (E)
returns the expression E under the name X
• Relations r

• r x  s (r)
Product of similar tables
(r,s) but different name
r x  s (r)=(r X s)= r X r
Set difference of two relations
• Relations r, s:

r – s:

In r set but not in s set


Set difference of two relations
• Relations student, employee:

Roll Name Emp.no Name


1 A 7 E
2 B 1 A
3 C

student Employee
The no. of columns in both the tables should be same
If Domain of attributes are same, then only difference operation take place
student - employee:
Roll Name
Find the name of the 2 B Column name should be
person who are student 3 C similar to the 1st table (left
but not employee hand side of the
difference operator)
Set difference of two relations
• Relations student, employee:

Roll Name Emp.no Name


1 A 7 E
2 B 1 A
3 C

student Employee
name (student) - name (employee): The person who are student but not
employee

Name
B
C
Joining two relations – Natural Join
• Let r and s be relations on schemas R and S
respectively.
Then, the “natural join” of relations R and S is a
relation on schema R  S obtained as follows:
– Consider each pair of tuples tr from r and ts
from s.
– If tr and ts have the same value on each of the
attributes in R  S, add a tuple t to the result,
where
• t has the same value as tr on r
• t has the same value as ts on s
Natural Join Example
• Relations r, s:
A B C C D

1 2 3 2 6

2 1 2 4 6

3 4 5

Natural Join
r s

 A, r.B, C, r.D, E ( r.C = s.C (r x s)))


Natural Join Example
• Relations r, s:
A B C C D

1 2 3 2 6

2 1 2 4 6

3 4 5

Natural Join
r s
A B R.C S.C D
1 2 3 2 6
 A, r.B, C, r.D, E ( r.C = s.C (r x s)))
1 2 3 4 6
2 1 2 2 6
2 1 2 4 6
3 4 5 2 6
3 4 5 4 6
Natural Join Example
• Relations r, s:
A B C C D

1 2 3 2 6

2 1 2 4 6

3 4 5

Natural Join
r s
A B R.C S.D D
1 2 3 2 6
 A, r.B, C, r.D, E ( r.C = s.C (r x s)))
1 2 3 4 6
2 1 2 2 6
2 1 2 4 6
3 4 5 2 6
3 4 5 4 6
Natural Join Example
• Relations r, s:
A B C C D

1 2 3 2 6

2 1 2 4 6

3 4 5

Natural Join
r s A B R.C D
2 1 2 6
 A, r.B, C, r.D, E ( r.C = s.C (r x s)))
Join
• Join is a special form of cross product of two tables.

• In Cartesian product we join a tuple of one table with the


tuples of the second table. But in join there is a special
requirement of relationship between tuples.

• For example, if there is a relation STUDENT and a relation


BOOK then it may be required to know that how many books
have been issued to any particular student. Now in this case,
the primary key of STUDENT that is stid is a foreign key in
BOOK table through which the join can be made.
Outer Join
• An outer join does not require each record in the two joined
tables to have a matching record.
• The joined table retains each record – even if no other
matching record exists.
• Outer joins subdivide further into left outer joins, right outer
joins, and full outer joins, depending on which table (s) one
retains the rows from (left, right, or both).
Left Outer Join
• The result of a left outer join or simply left join
for table A and B always contains all records of
left table A, even if the join-condition does not
find any matching record in the right table B.
• This means that if the ON clause matches 0
records in B, the join will still return a row in the
result, but with NULL in each column from B.
• If the right table returns one row and the left
table returns more than one matching row for it,
the values in the right table will be repeated for
each distinct row on the left table.
LEFT JOIN Tables
cid cname cemail oid orderdate amount cid

customers

select customers.cid, cname, amount from customers LEFT JOIN orders ON


customers.cid = orders.cid;
customers orders
customers.cid cname amount
Right Outer Join
• A right outer join or simply right join closely
resembles a left outer join, except with the
treatment of the tables reversed.
• Every row from the right table B will appear in
the joined table at least once.
• If no matching row from the left table A exists,
NULL will appear in columns from A for those
records that have no matching in B.
RIGHT JOIN Tables
cid cname cemail oid orderdate amount cid

customers orders

select customers.cid, cname, amount from customers RIGHT JOIN orders ON


customers.cid=orders.cid;

customers.cid cname amount


customers orders
Full outer join
• Conceptually, a full outer join combines the
effect of applying both left and right outer
joins.
• Where records in the full outer joined tables
do not match, the result set will have null
values for every column of the table that lacks
a matching row.
Full outer join
Name EmpID DeptName Dept
Employee
Harry 3415 Finance
Sally 2241 Sales
Name EmpID DeptName Mgr
George 3401 Finance
Harry 3415 Finance NULL
Harriet 2202 Sales
Sally 2241 Sales Harriet
Employee
George 3401 Finance NULL

DeptName Mgr Harriet 2202 Sales Harriet

Sales Harriet NULL NULL Production Charles

Production Charles
Select * from employee FULL
Dept
OUTER JOIN department ON
employee.DepatName=Dept.De
ptName
Anti Join
• The antijoin written as R ▷S where R and S are relations, is similar to the natural
join, but the result of an antijoin is only those tuples in R for which there is no
tuple in S that is equal on their common attribute names.
Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales DeptName EmpId DeptName
George 3401 Finance Harry 3415 Finance
Harriet 2202 Sales George 3401 Finance

Dept

DeptName Mgr
Sales Harriet
Production Charles
Semi Join
• The left semijoin is joining similar to the natural join and written as R⋉ S where R and S are
relation.
• The result of this semijoin is only the set of all tuples in R for which there is a tuple in S that is
equal on their common attribute names.

Employee
Name EmpID DeptName
Harry 3415 Finance
Sally 2241 Sales
Name EmpID DeptName
George 3401 Finance
Sally 2241 Sales
Harriet 2202 Sales
Harriet 2202 Sales
Dept

DeptName Mgr
Sales Harriet
Production Charles
Set intersection of two relations

• Relation r, s:

• rs

Note: r  s = r – (r – s) Common in r and s


Set intersection of two relations
• Relations student, employee:

Roll Name Emp.no Name


1 A 7 E
2 B 1 A
3 C

student Employee
name (student)  name (employee): The person who are student and also
employee

Name
A
Set Division of two relations
• Retrieve Sid of students who enrolled in every course. (at all, for all-division is
used)
Sid Cid Cid
S1 C1 C1
A(Sid,Cid)/B(Cid)
S2 C1 C2
Enrolled(Sid,Cid)/Course(Cid)
S1 C2 Course =Sid
S3 C2
Enrolled

• A(x,y)/B(y)= it results x values: for that there should be a tuple <x,y> in A for every
y value of relation B.
• Step 1 : Sid (Enrolled) X cid (Course)
It results all students are enrolled in every
S1 X C1 courses
S2 C2
S3
Set Division of two relations
• Retrieve Sid of students who enrolled in Step 2: Result
every course. (Sid (Enrolled) X cid (Course)) – Enrolled
Sid Cid Cid
S1 C1
S1 C1 C1 S1 C2
S2 C1 C2 S2 C1 S2 C2
S2 C2 = S3 C1
S1 C2 Course
S3 C1 It provides d the set of
S3 C2 S3 C2 students and their not
Enrolled enrolled courses
Step 3:
• Step 2:
Sid((Sid (Enrolled) X cid (Course)) – Enrolled)
(Sid (Enrolled) X cid (Course)) – Enrolled
S2
S1 C1 S1 C1
S3 It provides Sid of disqualified
Step 4: students
S1 C2 S2 C1
Sid (Enrolled)- (Sid((Sid (Enrolled) X cid (Course)) –
S2 C1 S1 C2 Enrolled))
S2 C2 S3 C2
S3 C1 S1 S2
S3 C2 S2 - S3 = S1 S1 enrolled in every course
S3
Composition of Operations
• Can build expressions using multiple operations
• Example: A=C (r x s)

• rxs

• A=C (r x s)
Summary of Relational Algebra Operators
Symbol (Name) Example of Use
σ
(Selection) σ salary > = 85000 (instructor)
Return rows of the input relation that satisfy the predicate.
Π
(Projection) Π ID, salary (instructor)
Output specified attributes from all rows of the input relation. Remove
duplicate tuples from the output.
x
(Cartesian Product) instructor x department

Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.

(Union) Π name (instructor) ∪ Π name (student)

Output the union of tuples from the two input relations.


-
(Set Difference) Π name (instructor) -- Π name (student)

Output the set difference of tuples from the two input relations.

(Natural Join) instructor ⋈ department

Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.

Database System Concepts - 6th Edition 2.43 ©Silberschatz, Korth and Sudarshan
MCQs
A_____ is a query that retrieves rows from more
than one table or view:
a) Start
b) End
c) Join
d) All of the mentioned
MCQs
A_____ is a query that retrieves rows from more
than one table or view:
a) Start
b) End
c) Join
d) All of the mentioned
MCQs
Which product is returned in a join query have
no join condition:
a) Equijoins
b) Cartesian
c) Both Equijoins and Cartesian
d) None of the mentioned
MCQs
Which product is returned in a join query have
no join condition:
a) Equijoins
b) Cartesian
c) Both Equijoins and Cartesian
d) None of the mentioned
MCQs
Which join refers to join records from the right
table that have no matching key in the left table
are include in the result set:
a) Left outer join
b) Right outer join
c) Full outer join
d) Half outer join
MCQs
Which join refers to join records from the right
table that have no matching key in the left table
are include in the result set:
a) Left outer join
b) Right outer join
c) Full outer join
d) Half outer join
MCQs
Relation R1 has 10 tuples and 5 attributes.
Relation R2 has 0 tuples and 7 attributes. When
a CROSS JOIN is achieved between R1 and R2,
how many tuples would the resultant set have?
A. 28
B. 10
C. 0
D. 35
MCQs
Relation R1 has 10 tuples and 5 attributes.
Relation R2 has 0 tuples and 7 attributes. When
a CROSS JOIN is achieved between R1 and R2,
how many tuples would the resultant set have?
A. 28
B. 10
C. 0
D. 35
MCQs
The ___________ operation, denoted by −,
allows us to find tuples that are in one relation
but are not in another.
a) Union
b) Set-difference
c) Difference
d) Intersection
MCQs
The ___________ operation, denoted by −,
allows us to find tuples that are in one relation
but are not in another.
a) Union
b) Set-difference
c) Difference
d) Intersection

You might also like