ITPEC IP April 2022 Question 27

Topic: DatabasesAlso asked in Spring 2020

There is the “Employee” table and the “Department” table in a relational database. The “Employee” table and the “Department” table are joined, and employees who live in prefectures different from the prefectures that their departments are located in are extracted. How many employees are extracted?

Employee

Em­ployee_​IDNameDepart­ment_​codePrefecture
H001Hanako ItoG02Kanagawa
H002Shiro TakahashiG01Kanagawa
H003Ichiro SuzukiG03Mie
H004Haruko TanakaG04Osaka
H005Jiro WatanabeG03Aichi
H006Saburo SatoG02Kanagawa

Department

Department_​codeDepartment_​namePrefecture
G01Administration DepartmentTokyo
G02Sales DepartmentKanagawa
G03Manufacturing DepartmentAichi
G04Development DepartmentOsaka

Explanation

2 employees — join each employee row to its department by Department_code, then compare the two Prefecture columns.

EmployeeLives inDepartmentDepartment inDifferent?
H001KanagawaG02KanagawaNo
H002KanagawaG01TokyoYes
H003MieG03AichiYes
H004OsakaG04OsakaNo
H005AichiG03AichiNo
H006KanagawaG02KanagawaNo

Only H002 (Shiro Takahashi) and H003 (Ichiro Suzuki) live outside their department's prefecture.

Answer (b)

Why not others:

  • •(a) 1 — stops after one mismatch; both H002 and H003 differ
  • •(c) 3 — no reading of the tables gives 3: the six employees split into 4 matching and 2 different
  • •(d) 4 — counts the employees whose prefecture is the same as the department's (H001, H004, H005, H006), the opposite condition

Key rule: A join pairs rows by the key value, not by position; compare other columns only after each row is paired with its own match.

Databases · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official IP past-paper collection or Report an issue.