ITPEC FE Morning April 2018 Question 29

Topic: Databases

When the SQL statement shown below is executed on the “Employee” table and the “Department” table, which of the following is the result?

SELECT COUNT(*) FROM Employee, Department
WHERE Employee.Department = Department.Department_name AND
Department.Floor = 2

Employee

Employee_numberDepartment
11001Administration
11002Accounting
11003Sales
11004Sales
11005Information systems
11006Sales
11007Planning
12001Sales
12002Information systems

Department

Department_nameFloor
Planning1
Administration1
Information systems2
Sales3
Accounting2
Legal affairs2
Procurement2

Explanation

3 — three employee rows join to a department on floor 2.

The WHERE clause joins each employee to its department and keeps only departments on floor 2:

  • •Floor 2 departments: Information systems, Accounting, Legal affairs, Procurement
  • •Matching employees: 11002 (Accounting), 11005 (Information systems), 12002 (Information systems)
  • •Legal affairs and Procurement have no employees, so they add no rows to the join

COUNT(*) counts the rows of the joined result: 3.

Answer (c)

Why not others:

  • •(a) 1 — too few: three employee rows satisfy both conditions, one in Accounting and two in Information systems
  • •(b) 2 — counts the distinct departments that have matching employees (Accounting and Information systems) instead of the employee rows
  • •(d) 4 — counts the floor 2 departments, including the two that have no employees and so drop out of the join

Key rule: COUNT(*) on a join counts the joined rows: a department with two employees contributes two rows, and a department with none contributes zero.

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 FE past-paper collection or Report an issue.