Relational Algebra Class Note
Relational Algebra Class Note
Relational Algebra Class Note
SELECT:
• σ<selection condition>(R)
• Sigma is the select statement
• Selection condition :
• Boolean expression (condition)
• Specified on the attributes of relation R
Unary Relational Operations
SELECT
SELECT:
SELECT * σDno=4(EMPLOYEE)
FROM EMPLOYEE
WHERE Dno=5
Unary Relational Operations
SELECT
SELECT * σSalary>=5000(EMPLOYEE)
FROM EMPLOYEE
WHERE Salary>=5000
Unary Relational Operations
SELECT
Select the tuples for all employees who either work in department 4 and
make over $25,000 per year, or work in department 5 and make over
$30,000,
SELECT *
FROM EMPLOYEE
• One has the needed columns (attributes) and contains the result of the
operation, and
• π<attribute list>(R)
• Example:
•πSex, Salary(EMPLOYEE)
• <‘F’, 25000> appears only once
• Even though this combination of values
appears twice in the EMPLOYEE relation. s
Unary Relational Operations
PROJECT
Example:
πSex, Salary(EMPLOYEE)
IN SQL it is:
SELECT DISTINCT Sex,Salary
FROM EMPLOYEE
Unary Relational Operations
PROJECT
TEMP ← σDno=5(EMPLOYEE)
R(First_name, Last_name, Salary) ← πFname, Lname,
Salary(TEMP)
Sequences of Operations and the
RENAME Operation
σDno=5(EMPLOYEE)
TEMP ←
Salary(TEMP)
!
!
Sequences of Operations and the
RENAME Operation
!
• Apply one operation at a time and create intermediate result relations.
• FEMALE_EMPS ← σSex=‘F’(EMPLOYEE)
EMPNAMES ← πFname, Lname, Ssn(FEMALE_EMPS)
EMP_DEPENDENTS ← EMPNAMES × DEPENDENT
ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)
RESULT ← πFname, Lname, Dependent_name(ACTUAL_DEPENDENTS)
The CARTESIAN PRODUCT (CROSS
PRODUCT) Operation
!
!
The CARTESIAN PRODUCT (CROSS
PRODUCT) Operation
!
!
The CARTESIAN PRODUCT (CROSS
PRODUCT) Operation
!
!
The CARTESIAN PRODUCT (CROSS
PRODUCT) Operation
!
!
• In SQL:
• CARTESIAN PRODUCT can be realized by using the CROSS
JOIN option in joined tables
• OR,
• If there are two tables in the WHERE clause and
• There is no corresponding join condition in the query,
• The result will also be the CARTESIAN PRODUCT of the two
tables
The JOIN operation
• Denoted by
• Used to combine related tuples from two relations into single
“longer” tuples.
• This operation is very important for any relational database with
more than a single relation because
• It allows us to process relationships among relations.
!
The JOIN operation
• Suppose that we want to retrieve the name of the manager of
each department.
• To get the manager’s name:
• Combine each department tuple with the employee tuple
whose Ssn value matches the Mgr_ssn value in the
department tuple.
• We do this by:
• Using the JOIN operation and then
• Projecting the result over the necessary attributes
The JOIN operation
• NATURAL JOIN
• denoted by *
• The standard definition of NATURAL JOIN requires that the two join attrib-
utes (or each pair of join attributes) have the same name in both relations.
• The attribute Dnum is called the join attribute for the NATURAL JOIN operation,
• because it is the only attribute with the same name in both relations.
• In the PROJ_DEPT relation, each tuple combines a PROJECT tuple with the
DEPARTMENT tuple for the department that controls the project, but only one join
attribute value is kept.
The EQUIJOIN & NATURAL JOIN
• If the attributes on which the natural join is specified already have the same
names in both relations, renaming is unnecessary.
• Combines each department with its locations and has one tuple for each location.
• In general, the join condition for NATURAL JOIN is constructed by equating each
pair of join attributes that have the same name in the two relations and combining
these conditions with AND.
• There can be a list of join attributes from each relation, and each corresponding
pair must have the same name.
The DIVISION Operation
• Denoted by ÷,
• Example: Retrieve the names of employees who work on all the projects that
‘John Smith’ works on.
1. Retrieve the list of project numbers that ‘John Smith’ works on in the
intermediate relation SMITH_PNOS:
SMITH ← σFname=‘John’ AND Lname=‘Smith’(EMPLOYEE)
1. whenever the employee whose Ssn is Essn works on the project whose
number is Pno in the intermediate relation SSN_PNOS:
3. Finally, apply the DIVISION operation to the two relations, which gives the
desired employees’ Social Security numbers: