# CST 222-2 Database Management Systems

2012

Tutorial Relational Algebra

All the Relation Algebra expressions base on the following database schema.

Page 1 of 3

CST 222-2 Database Management Systems

2012

Examples of Queries in Relational Algebra: Procedural Form
Retrieve the name and address of all employees who work for the µResearch¶ department. RESEARCH_DEPT DNAME=¶Research¶ (DEPARTMENT) RESEARCH_EMPS (RESEARCH_DEPT DNUMBER= DNOEMPLOYEEEMPLOYEE) RESULT FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

Retrieve the names of employees who have no dependents. ALL_EMPS SSN(EMPLOYEE) EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT) EMPS_WITHOUT_DEPS (ALL_EMPS - EMPS_WITH_DEPS) RESULT LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)

Examples of Queries in Relational Algebra ± Single expressions
Retrieve the name and address of all employees who work for the µResearch¶ department. Fname, Lname, Address ( Dname= µResearch¶ (DEPARTMENT Dnumber=Dno(EMPLOYEE))

Retrieve the names of employees who have no dependents. Lname, Fname(( Ssn (EMPLOYEE) í Ssn ( Essn (DEPENDENT))) EMPLOYEE)

Sample Relational Algebra expressions
For every project located in µColombo¶ list the project number, the controlling department number and the dept manager¶s last name, address and b¶date. Colombo_Projs <- Plocation= µColombo¶ (Project) Contr_Dept<-(Colombo_Projs Dnum=Dnumber Department)

Proj_Dept_Mgr <-(Contr_Dept MgrId=Empid Employee) Result<- Pnumber,Dnum,Lanme,Address,Bdate(Proj_Dept_Mgr)

Find the names of employees who work on all the projects controlled by dept. no. 5 Dept5_Projs(Pno)<- Pnumber( Dnum=5(Project))
Page 2 of 3

CST 222-2 Database Management Systems
Emp_Proj(Empid,Pno)<- Essn,Pno ( Works_on) Result_Emp_Empid<-(Emp_Proj÷Dept5_Proj) Result<- Lname,Fname(Result_Emp_Empid*Employee) Retrieve the name of employees who have no dependents. All_emps <- SSN(Employee) Emps_with_deps(SSN) <- ESSN(Dependent) Emps_without_deps<-(All_emps±Emps_with_deps) Result <- Lname, Fname (Emps_without_deps* Employee)

2012

List the names of managers who have at least one dependent. Mgrs(SSN) <- MGRSSN(Department) Emps_with_deps(SSN)<- ESSN(Dependent) Mgrs_with_deps <- (Mgrs Emps_with_deps) Result <- Lname, Fname (Mgrs_with_deps*Employee)

Page 3 of 3