Relational Algebra in Database
Relational Algebra in Database
Retrieval Update
Slid
e 5-
7
Select Operation
(DNO = 4 AND Salary > 25000) OR (DNO = 5 AND Salary > 30000) (EMPLOYEE)
OUTPUT
Select Operation
Selection condition is a Boolean expression specified on the
attributes of relation R
It can include boolean operators AND, OR, NOT applied on
relational operators < , > , <= , >= , != , =
Select is commutative:
<condition1>( < condition2> (R)) = <condition2> ( < condition1> (R))
Cascade of Select operations
<cond1>(< cond2> (<cond3>(R)) = <cond1> AND < cond2> AND < cond3>(R)))
(DNO = 4 AND Salary > 25000) OR (DNO = 5 AND Salary > 30000) (EMPLOYEE)
Project Operation (unary operation)
It selects a subset of columns from the relation.
denoted by <attribute list>R
It removes duplicate
tuples
Example:
The result of project
LNAME, FNAME, SALARY (EMPLOYEE) is set of tuples
OUTPUT
Project Operation
OUTPUT
Example 1
SALARY ( LNAME, FNAME, SALARY EMPLOYEE)
Example 2
LNAME, FNAME, SALARY ( SALARY EMPLOYEE)
NOW
Project operation is not commutative WHAT
???
Project Operation
Slid
e 5-
13
Relational Algebra Expressions
D5 DNO=5(EMPLOYEE)
R (First_name,Last_name,Salary) Fname, Lname, Salary D5
Union (Binary Operation)
The result of R S, is a relation that includes all
tuples that are either in R or in S or in both R and S
Duplicate tuples are eliminated
UNION Example
To retrieve the SSN of all employees who either
• work in department 5 or
• directly supervise an employee in department 5
19
RELATIONAL ALGEBRA OPERATIONS FROM
SET THEORY
Compatible relation
Student Instructor
Student Instructor
F SEX=’F’(EMPLOYEE)
EmpNames FNAME, LNAME, SSN (F)
Emp_DP EmpNames x DEPENDENT
Actual_DP SSN=ESSN(Emp_DP)
Result FNAME, LNAME, DEPENDENT_NAME(Actual_DP)
JOIN(Binary Operation)
R ⋈ <join condition>S
25
Example: JOIN operation
Retrieve the name of the manager of each department
26
RENAME OPEARATION
Rename operator is denoted by (rho)
D5 DNO=5(EMPLOYEE)
R (First_name, Last_name, Salary) Fname, Lname, Salary D5
RENAME OPEARATION
S(R) rename the relation R to S
S
R S = (R S ) – ((R - S) (S - R))
R ⋈ <join condition>S = <join condition> (R X S)
29
Some properties of JOIN
31
Equi-Join
EQUIJOIN is a join condition that involves only equality
operator = .
Example:
Retrieve a list of each female employee’s
dependents
FEmp SEX=’F’(EMPLOYEE)
E_DP FEmp ⋈ SSN=ESSN DEPENDENT
Result FNAME, LNAME, SSN,DEPENDENT_NAME
(E_DP)
32
This is EQUI -JOIN operation
Retrieve the name of the manager of each department
Slid
e 5-
34
Issue with Equijoin Operation
You have to specify the join condition.
Even if two cols in the joining tables have same name.
Superfluous column
36
NATURAL JOIN Operation
Example: Print location of each department
DEPT_LOCS DEPARTMENT * DEPT_LOCATIONS
Only attribute with the same name is DNUMBER
37
Example: Natural Join
Consider two Relations A B C D C D E
39
Theta-join Example
For each Male employee, list his colleagues who
earn more than him. Retrieve only the first
name and salary.
M(Name, Sal) FNAME, SALARY ( SEX=’M’ EMPLOYEE )
ECOL(CName, CSal) FNAME, SALARY EMPLOYEE
R1 M ⋈ M.Sal < ECol.CSal ECol
40
Theta-join Example
For each Male employee list his colleagues who earn more
than him. Retrieve only the first name and salary.
M(Name, Sal) FNAME, SALARY ( SEX=’M’ EMPLOYEE )
ECOL(CName, CSal) FNAME, SALARY EMPLOYEE
R1 M ⋈ M.Sal < ECol.CSal ECol
Name Sal CName CSal
John 30000 Franklin 40000
John 30000 Jennifer 43000
John 30000 Ramesh 38000
John 30000 James 55000
Franklin 40000 Jennifer 43000
Franklin 40000 James 55000
Ramesh 38000 Franklin 40000
41
Ramesh 38000 Jennifer 43000
Ramesh 38000 James 55000
Theta-join
For each Male employee, print the names of his peers
with the same salary
E2( EMPLOYEE )
E2 FNAME, SALARY ( SEX=’M EMPLOYEE )
Res E1.FNAME, E2.FNAME ( E1 ⋈E1.SSN != E2.SSN and
E1.Salary=E2.Salary E2)
42
Aggregate Functions
Mathematical Aggregate Functions applied to
collections of numeric values include
SUM, AVERAGE, MAXIMUM, and MINIMUM.
COUNT function is used for counting tuples or values.
Examples:
Retrieve the average or total salary of all employees
Retrieve total number of employee tuples
44
ℱMAX Salary (EMPLOYEE)
Aggregate ℱMIN Salary (EMPLOYEE)
Functions ℱ ℱSUM Salary, AVERAGE Salary (EMPLOYEE)
ℱCOUNT SSN (EMPLOYEE)
45
Using Grouping with Aggregation
Grouping can be combined with Aggregate Functions
Example:
For each department, retrieve the DNO, COUNT of
employees and AVERAGE SALARY
DNO ℱCOUNT SSN, AVERAGE Salary EMPLOYEE
46
Grouping with Aggregation
DNO ℱCOUNT SSN, AVERAGE Salary EMPLOYEE
47
Grouping with Aggregation
DNO ℱCOUNT SSN, AVERAGE Salary EMPLOYEE
48
Examples of Queries in RA
Retrieve the name and address of all employees who work for
the ‘Research’ department.
50
EXAMPLE: RETRIEVE THE NAMES OF ALL EMPLOYEES
WITH TWO OR MORE DEPENDENTS.
T1(Ssn, No_of_dependents) Essn ℱ COUNT Dependent_name (DEPENDENT)
T2 No_of_dependents >1(T1)
RESULT LNAME, FNAME (T2 * EMPLOYEE)
51
Outer Join Operation
In INNER JOIN, tuples without a matching are
eliminated from the join result
Tuples with null are also eliminated
This amounts to loss of information.
53
Left Outer Join
List the employees name and the department name
that they manage. If they don’t manage one, then
indicate this with a null value.
Temp (Employee Ssn=Mgr_Ssn Department)
Result Fname, Minit, Lname, Dname(Temp)
54
Right Outer Join
List the employees name and the department name that
they manage. If they don’t manage one, then indicate this
with a null value.
Temp (Department Mgr_Ssn= Ssn Employee)
Result Fname, Minit, Lname, Dname(Temp)
55
Full Outer Join
List the employees name and the department name that they
manage. If they don’t manage one or the department have
no manager, then indicate this with a null value.
58
Inner and Outer Joins
Slid
e 8-
59
Another Example Outer Join
List the employees name and the Project name that they
work on. If they don’t work on any project or a project
have no employee working on it, then indicate this with
a null value.
Yet another Example
Find SSN of employees who work on all the projects of
Dnum= 4
DIVISION
Yet an other Example
Find SSN of employees who work on all the projects of
Dnum= 4
PD4
Pno
10
30
DIVISION
DIVISION (Binary Operation)
Division operation is applied to two relations R1 and R2
R1(Attr_R1) R2(Attr_R2)
where Attr_R2 Attr_R1.
Let Result = R1 R2
Attr_Result = Attr_R1 - Attr_R2
Attr_Result is a set of attributes of R1
that are not the attributes of R2.
For a tuple to appear in the result of the DIVISION, the values in t must63
appear in R1 in combination with every tuple in R2.
Example of DIVISION
Find SSN of employees who work on all the projects that
John Smith works on
R S
Temp B( ( B(R) x S) – R)
T B(R) - Temp
Example
For every project located in ‘Stafford’, list the project no, the
controlling department no, and the department manager’s
last name, address, and birth date.
67
Example of Query Tree
For every project located in ‘Stafford’, list the project no, the
controlling department no, and the department manager’s
last name, address, and birth date.
68
Leaf nodes represent base
relations
Query Tree
We can retrieve employees at each level and then take their union,
however, we cannot specify a query such as
71
PRACTICE QUESTION
Do example queries and the questions at the
end of Relational Algebra Chapter in
DIVISION