25-Comp-B3 Data Bases and File Systems · December 2017
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
deptId (FK → Department.id), dropped from the printed schema; "department 10" means the Department row whose deptNo=10. This is the minimal schema change that makes part (a) answerable without altering (b)/(c).Given. Employee(id, name, salary, deptId); Manages(emp_id, mgr_id) — one row per employee, emp_id is the key, mgr_id is that employee's one direct/immediate manager; Department(id, deptNo). The "transitive manager" sentence is background motivating why Manages need only store the immediate manager (transitive managers are derivable, not a modelling requirement for these three queries).
Find. Three SQL queries: (a) IDs of employees in the department whose deptNo is 10; (b) name+salary of every employee earning more than their own immediate manager; (c) name+ID of the one employee with no row in Manages (the CEO).
Approach. (a) is a simple two-table equi-join filtered on deptNo. (b) is a 3-way self-join through Manages: join Employee to itself via Manages so each row pairs an employee with their manager's own Employee row, then filter on the salary comparison. (c) is an anti-join: an employee is the CEO exactly when their id never appears as an emp_id in Manages (everyone else has exactly one Manages row).
SELECT E.id
FROM Employee E, Department D
WHERE E.deptId = D.id
AND D.deptNo = 10;
Equivalently with explicit JOIN syntax: SELECT E.id FROM Employee E JOIN Department D ON E.deptId = D.id WHERE D.deptNo = 10; — both forms are accepted; the exam-era convention (comma-join + WHERE) is shown first since the source paper is closed-book/pre-ANSI-JOIN style throughout.SELECT E.name, E.salary
FROM Employee E, Manages M, Employee Mgr
WHERE E.id = M.emp_id
AND M.mgr_id = Mgr.id
AND E.salary > Mgr.salary;
Only the IMMEDIATE manager (M.mgr_id) is compared, per part (b)'s own wording — the transitive-manager fact from the preamble is irrelevant here.SELECT name, id
FROM Employee
WHERE id NOT IN (SELECT emp_id FROM Manages);
NOT IN is safe here specifically because emp_id in Manages is declared NOT NULL (it is that table's key) — with a nullable subquery column, NOT IN silently returns no rows if any NULL is present, so a NOT EXISTS correlated form (WHERE NOT EXISTS (SELECT 1 FROM Manages M WHERE M.emp_id = Employee.id)) is the more defensively-written equivalent and would be accepted equally.| Part | Query technique |
|---|---|
| (a) | Employee &Join; Department on deptId=id, filter deptNo=10 |
| (b) | Employee &Join; Manages &Join; Employee(as Mgr), filter E.salary > Mgr.salary |
| (c) | Employee.id NOT IN (SELECT emp_id FROM Manages) — anti-join for "has no manager" |