ITPEC FE Subject A October 2024 Question 20

Topic: DatabasesAlso asked in April 2019

An employee works for a department, which can be located in multiple regions. Three tables EMP, DEPT, and DEPT_LOCS are created as shown below for recording the employee, department, and department location data, respectively.

EMP

EIDEnameDNOSalary
11John Bate120000
12Mohammed Karim240000
13Sadat Hossain150000
14Katherine Li320000
15Shuvashish Bose340000

DEPT

DNODnameManagerID
1Admin11
2Accounts13
3Research15

DEPT_LOCS

DNORegion
1L1
1L3
2L2
3L3
3L2

What is the output of the SQL shown below?

SELECT EName, Salary
FROM EMP
WHERE DNO IN (( SELECT DNO
FROM DEPT)
MINUS
(SELECT DNO
FROM DEPT_LOCS
WHERE Region=’L2’)
))

Explanation

John Bate 20000 and Sadat Hossain 50000 — MINUS leaves only department 1, the one department with no location in region L2.

  • •SELECT DNO FROM DEPT → {1, 2, 3}
  • •SELECT DNO FROM DEPT_LOCS WHERE Region = 'L2' → {2, 3}: department 2 is in L2, department 3 in L3 and L2
  • •MINUS: {1, 2, 3} − {2, 3} = {1}

The outer query returns the employees with DNO 1: John Bate (20000) and Sadat Hossain (50000).

Answer (b)

Why not others:

  • •(a) NULL — the difference {1} is not empty; and an empty subquery result would return no rows, not a row of NULLs
  • •(c) — keeps Katherine Li and Shuvashish Bose of department 3, but department 3 has a location in L2 and is removed, even though it is also in L3
  • •(d) — all five employees, as if the MINUS removed nothing and the filter were just SELECT DNO FROM DEPT

Key rule: MINUS (standard SQL EXCEPT) keeps the values of the first query that never appear in the second; a department with any row in L2 is excluded, whatever its other regions.

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.