Example of Queries on Relational Algebra

Relational Algebra provides the theoretical foundation of querying. It is used for understanding and manipulating data in relational databases. It is used for a variety of operations to query and transform datasets effectively. By understanding these operations, database users can perform complex queries while maintaining clarity and precision.

In this chapter, we will see several query examples from relational algebra. Each example is based on a specific scenario and demonstrates the practical application of relational algebra operations such as selection, projection, join, union, and division.

Let us first take a look at the required tables and their data to express the query in true sense.

EMPLOYEE Table

Here’s the EMPLOYEE table that collects the data relevant to all employees –

FnameMinitLnameSsnBdateAddressSexSalarySuper_ssnDno
JohnBSmith1234567891965-01-09731 Fondren, Houston, TXM300003334455555
FranklinTWong3334455551955-12-08638 Voss, Houston, TXM400008886655555
AliciaJZelaya9998877771968-01-193321 Castle, Spring, TXF250009876543214
JenniferSWallace9876543211941-06-20291 Berry, Bellaire, TXF430008886655554
RameshKNarayan6668844441962-09-15975 Fire Oak, Humble, TXM380003334455555
JoyceAEnglish4534534531972-07-315631 Rice, Houston, TXF250003334455555
AhmadVJabbar9879879871969-03-29980 Dallas, Houston, TXM250009876543214
JamesEBorg8886655551937-11-10450 Stone, Houston, TXM55000NULL1

DEPARTMENT Table

There are three departments as highlighted in the following DEPARTMENT table –

DnameDnumberMgr_ssnMgr_start_date
Research53334455551988-05-22
Administration49876543211995-01-01
Headquarters18886655551981-06-19

PROJECT Table

The PROJECT table contains the relevant data of all the projects –

PnamePnumberPlocationDnum
ProductX1Bellaire5
ProductY2Sugarland5
ProductZ3Houston5
Computerization10Stafford4
Reorganization20Houston1
Newbenefits30Stafford4

WORKS_ON Table

The WORKS_ON table gathers which employee is working on which project for how many hours –

EssnPnoHours
123456789132.5
12345678927.5
666884444340.0
453453453120.0
453453453220.0
333445555210.0
333445555310.0
3334455551010.0
3334455552010.0
9998877773030.0
9998877771010.0
9879879871035.0
987987987305.0
9876543213020.0
9876543212015.0
88866555520NULL

DEPENDENT Table

The DEPENDENT table is as follows –

EssnDependent_nameSexBdateRelationship
333445555AliceF1986-04-05Daughter
333445555TheodoreM1983-10-25Son
333445555JoyF1958-05-03Spouse
987654321AbnerM1942-02-28Spouse
123456789MichaelM1988-01-04Son
123456789AliceF1988-12-30Daughter
123456789ElizabethF1967-05-05Spouse

Query Examples on Relational Algebra

Let us now check some queries that extracts data from the inputs supplied in the above tables –

Retrieve the Name and Address of Employees in the Research Department

This query is to identify employees working in the “Research” department and retrieve their names and addresses.

Steps − Use a selection operation to filter the “Research” department from the DEPARTMENT relation –

RESEARCHDEPT←σDname=′Research′(DEPARTMENT)

DnameDnumberMgr_ssnMgr_start_date
Research53334455551988-05-22

Join the resulting relation with the EMPLOYEE relation using the department number (Dnumber and Dno) –

RESEARCHEMPS←RESEARCHDEPT⋈Dnumber=DnoEMPLOYEE

DnameDnumberMgr_ssnMgr_start_dateFnameMinitLnameSsnBdateAddressSexSalarySuper_ssnDno
Research53334455551988-05-22JohnBSmith1234567891965-01-09731 Fondren, Houston, TXM300003334455555
Research53334455551988-05-22FranklinTWong3334455551955-12-08638 Voss, Houston, TXM400008886655555
Research53334455551988-05-22RameshKNarayan6668844441962-09-15975 Fire Oak, Humble, TXM380003334455555
Research53334455551988-05-22JoyceAEnglish4534534531972-07-315631 Rice, Houston, TXF250003334455555

Finally, apply a projection operation to retrieve the desired attributes (first name, last name, and address).

RESULT←πFname,Lname,Address(RESEARCHEMPS)

FnameLnameAddress
JohnSmith731 Fondren, Houston, TX
FranklinWong638 Voss, Houston, TX
RameshNarayan975 Fire Oak, Humble, TX
JoyceEnglish5631 Rice, Houston, TX

In-Line Expression −

πFname,Lname,Address(σDname=′Research′(DEPARTMENT⋈Dnumber=DnoEMPLOYEE))

List Project Details for Stafford

For projects located in “Stafford,” we are going to retrieve the project number, the controlling department, and the manager’s details (last name, address, and birth date).

Steps − Filter projects located in “Stafford” using selection.

STAFFORD_PROJS←σPlocation=′Stafford′(PROJECT)

PnamePnumberPlocationDnum
Computerization10Stafford4
Newbenefits30Stafford4

Join these projects with their controlling departments based on the department number –

CONTR_DEPTS←STAFFORD_PROJS⋈Dnum=DnumberDEPARTMENT

PnamePnumberPlocationDnumDnameDnumberMgr_ssnMgr_start_date
Computerization10Stafford4Administration49876543211995-01-01
Newbenefits30Stafford4Administration49876543211995-01-01

Join the result with the EMPLOYEE relation to retrieve manager details (Mgr_ssn = Ssn) –

PROJ_DEPT_MGRS←CONTR_DEPTS⋈Mgr_ssn=SsnEMPLOYEE

PnamePnumberPlocationDnumDnameDnumberMgr_ssnMgr_start_dateFnameMinitLnameSsn
Computerization10Stafford4Administration49876543211995-01-01JenniferSWallace987654321
Newbenefits30Stafford4Administration49876543211995-01-01JenniferSWallace987654321

Finally, use projection to extract the desired attributes.

RESULT←πPnumber,Dnum,Lname,Address,Bdate(PROJ_DEPT_MGRS)

PnumberDnumLnameAddressBdate
104Wallace291 Berry, Bellaire, TX1941-06-20
304Wallace291 Berry, Bellaire, TX1941-06-20

Employees Working on All Projects Controlled by Department 5

This query identifies employees assigned to every project under department number 5.

Steps − Create a relation with project numbers of all projects controlled by department 5 –

DEPT5_PROJS←ρ(Pno)(πPnumber(σDnum=5(PROJECT)))

Pno
1
2
3

Build a relation of employees and their assigned projects (Ssn and Pno).

EMP_PROJ←ρ(Ssn,Pno)(πEssn,Pno(WORKS_ON))

SsnPno
1234567891
1234567892
6668844443
4534534531
4534534532
3334455552
3334455553
33344555510
33344555520
99988777730
99988777710
98798798710
98798798730
98765432130
98765432120
88866555520

Apply the division operation to find employees associated with all DEPT5_PROJS.

RESULT_EMP_SSNS←EMP_PROJ÷DEPT5_PROJS

Join with the EMPLOYEE relation to retrieve names.

RESULT←πLname,Fname(RESULT_EMP_SSNS∗EMPLOYEE)

Project Numbers Involving Smith (As Worker or Manager)

We want to identify all projects where an employee named “Smith” is involved. This is either as a worker or as a department manager.

Steps − Retrieve the Social Security Numbers (SSNs) of employees named “Smith” –

SMITHS(Essn)←πSsn(σLname=′Smith′(EMPLOYEE))

Essn
123456789

Use the Cartesian product to find projects where “Smith” works.

SMITH_WORKER_PROJS←πPno(WORKS_ON∗SMITHS)

EssnPnoHours
123456789132.5
12345678927.5

Retrieve SSNs of department managers from the DEPARTMENT relation –

MGRS←πLname,Dnumber(EMPLOYEE⋈Ssn=Mgr_ssnDEPARTMENT)

LnameDno
Wong5
Wallace4
Borg1

Find the departments managed by “Smith” and join them with PROJECT to identify projects –

SMITH_MANAGED_DEPTS(Dnum)←πDnumber(σLname=′Smith′(MGRS))

SMITH_MGR_PROJS(Pno)←πPnumber(SMITH_MANAGED_DEPTS∗PROJECT)

Combine the two project lists using union –

RESULT←(SMITH_WORKER_PROJS∪SMITH_MGR_PROJS)

Employees with Two or More Dependents

The goal is to list employees who have at least two dependents. This requires using aggregate functions, which go beyond basic relational algebra.

Steps − Count the number of dependents for each employee using COUNT –

T1(Ssn,No_of_dependents)←EssnℑCOUNTDependent_name(DEPENDENT)

Filter employees with more than two dependents –

T2←σNo_of_dependents>2(T1)

Join with EMPLOYEE to get their names –

RESULT←πLname,Fname(T2∗EMPLOYEE)

Employees without Dependents

This query retrieves employees who have no dependents.

Steps − Create a relation with all employee SSNs.

ALL_EMPS←πSsn(EMPLOYEE)

Create a relation of employees with dependents.

EMPS_WITH_DEPS(Ssn)←πEssn(DEPENDENT)

Use the set difference to find employees with no dependents.

EMPS_WITHOUT_DEPS←(ALLEMPS–EMPS_WITH_DEPS)

Join with EMPLOYEE to retrieve names.

RESULT←πLname,Fname(EMPS_WITHOUT_DEPS∗EMPLOYEE)

Managers with Dependents

This query lists managers who have at least one dependent.

Steps − Extract SSNs of department managers –

MGRS(Ssn)←πMgrssn(DEPARTMENT)

Extract SSNs of employees with dependents –

EMPS_WITH_DEPS(Ssn)←πEssn(DEPENDENT)

Use intersection to find managers with dependents –

MGRS_WITH_DEPS←(MGRS∩EMPS_WITH_DEPS)

Retrieve their names –

RESULT←πLname,Fname(MGRS_WITH_DEPS∗EMPLOYEE)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *