Skip to main content

RA: computer dept employees and supervisors

Short answer

Identify computer department employees via project and work joins. Rename emp as supervisor relation, join on sup_eid = eid, and project employee name and supervisor name.

Consider the relational schema: emp (eid, name, add, sup_eid) dep (dep_id, name) project (pid, title, dep_id) work (eid, pid, hours) List name of all employees from computer dept. along with name of their supervisor. (Write a relational algebra expression.)

Relational algebra symbols used: σ select, π project, ⋈ natural join, ∪ union, ∩ intersection, − difference, ρ rename, 𝒢 aggregate.

Both emp and dep have an attribute called name, and emp refers to itself through sup_eid, so renaming is needed. Assumption: an employee belongs to the Computer department if he or she works on a project of that department (emp has no dep_id of its own).

Relational algebra expression

C  = π eid ( work ⋈ project ⋈ σ name = "Computer" (dep) )
E  = ρ E ( emp ⋈ C )                       -- computer employees, as E
S  = ρ S (eid, sname, sadd, ssup) ( emp )  -- the same relation as supervisors

π E.name, S.sname ( σ E.sup_eid = S.eid ( E × S ) )

or as a join: π E.name, S.sname ( E ⋈ E.sup_eid = S.eid S ).

Explanation

  1. σ name="Computer"(dep) picks the department; joining with project (on dep_id) and work (on pid) finds the eid of employees working on its projects.
  2. emp ⋈ C restricts emp to those employees.
  3. emp is renamed a second time as S so that the supervisor of an employee can be matched, E.sup_eid = S.eid.
  4. The projection prints the employee name and the supervisor name.

Equivalent SQL (tested)

SELECT e.name, s.name
FROM emp e JOIN emp s ON e.sup_eid = s.eid
WHERE e.eid IN (SELECT eid FROM work JOIN project USING(pid)
                JOIN dep USING(dep_id) WHERE dep.name = 'Computer');

✓ Answer checked by Claude Opus 5.5 (high effort)

All Database Management System chapter 3 questions →