Which of the following SQL queries deletes all tuples in the teacher relation…
2023
Which of the following SQL queries deletes all tuples in the teacher relation for teachers associated with a department located in the building named 'CSE', as recorded in the department relation?
- A.
DELETE FROM teacherWHERE dept_name IN 'CSE'; - B.
DELETE FROM departmentWHERE building = 'CSE'; - C.
DELETE FROM teacherWHERE dept_name IN (SELECT dept_nameFROM departmentWHERE building = 'CSE'); - D.
None of the above
Attempted by 68 students.
Show answer & explanation
Correct answer: C
A DELETE statement's WHERE clause can only test columns belonging to the table it deletes from. When the filtering condition instead depends on an attribute stored in a DIFFERENT relation, the standard, portable way to express this is a subquery: first resolve the qualifying values from that other relation, then match the outer DELETE's WHERE...IN clause against the resulting set. (Some dialects also allow a correlated EXISTS or a joined DELETE/USING form for the same logic, but the subquery form is the one offered here.)
The inner subquery,
SELECT dept_name FROM department WHERE building = 'CSE', resolves to the set of department names whose department is located in the CSE building.The outer query,
DELETE FROM teacher WHERE dept_name IN (...), then removes every teacher row whose dept_name is a member of that resolved set -- this is the query that satisfies the requirement.
Checking why every other option fails:
A query that compares dept_name directly to the building's name 'CSE' mixes two different attributes from two different relations, and is not a syntactically valid IN condition either way.
A query that deletes rows from the department relation itself removes department records, not the associated teacher records the question asks for.
Since a valid query does satisfy the requirement, the option stating that none of the given queries works does not hold.
The subquery-based DELETE is therefore the query, among those offered, that correctly performs the required deletion.