Management & CommerceVery hardBusiness analyst · SQL test

Table emp has ids 1–6. Table blocked(id) has two rows: 2 and NULL. What do these return? P: SELECT COUNT(*) FROM emp WHERE id NOT IN (SELECT id FROM blocked); Q: SELECT COUNT(*) FROM emp e WHERE NOT EXISTS (SELECT 1 FROM blocked b WHERE b.id = e.id);

  1. A. P = 0, Q = 5
  2. B. P = 5, Q = 5
  3. C. P = 0, Q = 0
  4. D. P = 5, Q = 6

Correct answer: A. P = 0, Q = 5

For id NOT IN (2, NULL), every comparison with NULL is unknown, so the condition is never true and P returns 0. NOT EXISTS only checks for matching rows, so it excludes id 2 and returns 5. This is why NOT EXISTS is safer when the subquery can contain NULLs.

More Operations, Supply Chain & Business Analytics questions

Exams that ask this topic