CS FundamentalsVery hardAmazon/Flipkart SQL round · GATE CS

Table Dept(did PRIMARY KEY) rows: (10), (20), (30). Table Emp(eid PRIMARY KEY, did REFERENCES Dept(did) ON DELETE CASCADE, mentor REFERENCES Emp(eid) ON DELETE SET NULL) rows: (1, 10, NULL), (2, 10, 1), (3, 20, 1), (4, 20, 3), (5, 30, 2), (6, 30, 4). What does this query return?

Table Dept(did PRIMARY KEY) rows: (10), (20), (30). Table Emp(eid PRIMARY KEY, did REFERENCES Dept(did) ON DELETE CASCADE, mentor REFERENCES Emp(eid) ON DELETE SET NULL) rows: (1, 10, NULL), (2, 10, 1), (3, 20, 1), (4, 20, 3), (5, 30, 2), (6, 30, 4). What does this query return?
DELETE FROM Dept WHERE did = 10;
SELECT COUNT(*), COUNT(mentor) FROM Emp;
  1. A. 6, 4
  2. B. 4, 3
  3. C. 4, 4
  4. D. 4, 2

Correct answer: D. 4, 2

Deleting department 10 cascades to employees 1 and 2. Their deletion triggers ON DELETE SET NULL on mentor for employees 3 (mentor 1) and 5 (mentor 2). Remaining: 3, 4, 5, 6; only 4 and 6 still have mentors.

More DBMS and SQL questions

Exams that ask this topic