Professional Documents
Culture Documents
EXCERCISES
READ THE NOTES BELOW AND ANSWER THE QUESTIONS
PLEASE BRING THIS NOTES TO NEXT CLASS
Relational algebra operators operate on Relations (or tables) to produce new relations. In
this method data retrieval functions are specified by a procedural language. It is based on
algebraic concepts that consist of a collection of operators that are defined upon relations
and produce as result other relations.
We shall see some basic operators like: Selection, Projection, Union, Intersection,
Difference, Cartesian product and Join.
1. Selection:
When applied on a relation, this operator results in a horizontal (selected row-wise)
selection of a portion of that relation.
Consider the following relation: EMPLOYEE
EMPLOYEE
NAME SEX AGE SALARY
Mustafa Male 25 2000
Salima Female 40 3000
Ahad Male 36 4500
Muneera Female 28 3800
The selection operation of Age < 30 or Salary > 4000 gives the following relation:
EMPLOYEE
NAME AGE SALARY
EMPLOYEE
NAME AGE SALARY
EMPLOYEE
NAME AGE SALARY
Mustafa 25 2000
28
Ahad 36 4500
Muneera 28 3800
2. Projection
Projection gives a vertical (selected columns only) selection of a portion of a relation as
the result.
EMPLOYEE
NAME DEPARTMENT MANAGER
Maryam Sales Ali
Latifa Sales Ali
Mustafa Marketing Saif
Mohammed Accounts Mansoor
The projection attributes name and head from the above EMPLOYEE relation would be
as follows:
EMPLOYEE
NAME MANAGER
Maryam Ali
Latifa Ali
Mustafa Saif
Mohammed Mansoor
3. Union
This operator when applied on two relations NATIONALS and MANAGERS results in a
combination of a new relation. Remember to remove duplicates if any.
Consider the following relations: NATIONALS and MANAGERS
NATIONALS
ID NAME AGE
145 Mohammed 25
238 Ali 40
29
650 Anisa 36
MANAGERS
ID NAME AGE
99 Ali 52
232388 Ali 39
650650 Anisa 26
NATIONALS ∪ MANAGERS
ID NAME AGE
145 Mohammed 25
238 Ali 40
650 Anisa 36
99 Ali 52
4. Intersection
This operator when applied on two relations GRADUATES and MANAGERS results in a
perfectly matching combination of a new relation.
Consider the following relation: GRADUATES and MANAGERS
5. Difference
This operator when applied on two relations ARABS and MANAGERS results in a
subtraction of rows to result in a new relation.
30
ARABS
ID NAME AGE
145 Mohammed 25
238 Ali 40
650 Anisa 36
MANAGERS
ID NAME AGE
99 Ali 52
238 Ali 39
650 Anisa 26
ARABS - MANAGERS
ID NAME AGE
145 Mohammed 25
It is like a Subtraction – all records in the first relation subtract all records in the second
relation.
6. Cartesian Product
It is also called the Product operation. It takes two relations and returns a relation where
every row of the first relation is concatenated with every Tuple in the second relation.
Consider the following two relations:
EMPLOYEE
NAME DEPT GRADE
Maryam Sales 4
Latifa Sales 4
Mustafa Marketing 5
Mohammed Accounts 3
DEPARTMENT
DEPT_NAME MANAGER BUDGET
Sales Ali 1,200,000
Marketing Saif 50,000
Accounts Mansoor 50,000,000
Now the first relation has 3 columns and the second relation has 3 columns. The resulting
relation has 3+3=6 columns. Each row of relation EMPLOYEE appears with each
relation of DEPARTMENT and so there are 4x3=12 records shown below:
EMPLOYEE product DEPARTMENT
NAME DEPT GRADE DEPT_NAME MANAGER BUDGET
Maryam Sales 4 Sales Ali 1,200,000
31
Maryam Sales 4 Marketing Saif 50,000
Maryam Sales 4 Accounts Mansoor 50,000,000
Latifa Sales 4 Sales Ali 1,200,000
Latifa Sales 4 Marketing Saif 50,000
Latifa Sales 4 Accounts Mansoor 50,000,000
Mustafa Marketing 5 Sales Ali 1,200,000
Mustafa Marketing 5 Marketing Saif 50,000
Mustafa Marketing 5 Accounts Mansoor 50,000,000
Mohammad Accounts 3 Sales Ali 1,200,000
Mohammad Accounts 3 Marketing Saif 50,000
Mohammad Accounts 3 Accounts Mansoor 50,000,000
Summary :
We know that in mathematics, a set can have any number of subsets. A set is the subject
of another if all its members are also members of other set. For example, consider the
following two sets:
We can state here that S is a subset of S because all the elements of S are also the
elements of S.
32
As in the case of set theory in mathematics, we can treat a table as a set and derive some
rows from that table as its subset. In effect, we will have another table with the same
header, but with a different name. For example, from a student table, we can derive
another table that contains records for only those students who have passed. Thus we
restrict the number of rows that we want.
The idea in a restrict operation is to select only the desired rows and eliminate the
unwanted rows.
Union In mathematical set theory, the union of two sets is a set that contains elements
present in either or both of the sets. In the process, any resulting duplicates are
eliminated.
By using union, multiple queries can be put together and their output merged.
Intersection In mathematics, the intersection of two sets is the set of elements that are
common to both the sets.
The result of intersection is the rows common to the rows produced by the individual
queries.
In mathematics, the difference between two sets is a set that contains members of the first
set that are not in the second set.
Join joins two or more tables by using operators such as equal to, not equal to, greater
than etc.
Joining two (or more) tables has some prerequisites, if it has to be of some practical
value:
33
TUTORIAL 4
a. Selection
b. Projection
c. Union
d. Difference
PROJECT
PROJ_NAME LEADER LOCATION
Sultan Centre Ali Azaiba
Markat Al Bahja Saif Seeb
Villa Mansoor Qurum
PROJECT
STAFF_NAME PROJ_NAME TIME
Ahmed Sultan Centre 2 months
Bader Villa 1 month
Khalfan Villa 3 months
34