Skip to main content

RA: projects where all used bolts size 6

Short answer

Use difference: from all projects with workers, subtract those having any employee who did not use bolts size 6, then join project for name: π p_name (project ⋈ (π p_no(work) − π p_no(work − π e_no,p_no(use ⋈ σ part_name='bolts'∧size=6(part)))).

Consider the relational schema: employee (e_no, name, address) project (p_no, p_name) work (e_no, p_no) part (part_no, part_name, qty, size) use (e_no, p_no, part_no, number) List names of projects such that every employee working on these projects has used a part "bolts" with size 6. (Write a relational algebra expression.)

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

The question needs "every employee of the project", so it is a "for all" query. It is solved with difference (or the division operator). Assumption: an employee counts only if he or she used the part on that same project.

Relational algebra expression

Step by step:

B    = π part_no ( σ part_name = "bolts" ∧ size = 6 (part) )
Good = π e_no, p_no ( use ⋈ B )          -- (employee, project) pairs that used size-6 bolts
Bad  = π p_no ( work − Good )            -- projects having some employee who did not
All  = π p_no ( work )                   -- projects having at least one employee
Ans  = π p_name ( project ⋈ ( All − Bad ) )

In one line:

π p_name ( project ⋈ ( π p_no (work) −
    π p_no ( work − π e_no, p_no ( use ⋈ π part_no ( σ part_name="bolts" ∧ size=6 (part) ) ) ) ) )

Explanation

  • work − Good leaves the employees who work on a project but have not used size-6 bolts there.
  • Their projects (Bad) are removed from all projects, so only projects in which every employee used the bolts remain.

Equivalent SQL (tested)

SELECT p.p_name FROM project p
WHERE p.p_no IN (SELECT p_no FROM work)
AND NOT EXISTS (
  SELECT 1 FROM work w WHERE w.p_no = p.p_no AND NOT EXISTS (
    SELECT 1 FROM use u JOIN part t ON t.part_no = u.part_no
    WHERE u.e_no = w.e_no AND u.p_no = w.p_no
      AND t.part_name = 'bolts' AND t.size = 6));

✓ Answer checked by Claude Opus 5.5 (high effort)

All Database Management System chapter 3 questions →