Question 1
The `employees` table has a self-referencing `manager_id` column that is NULL for employees who report to nobody, and every non-NULL `manager_id` matches an existing `emp_id`. Which query lists the last name of every employee who is **not** the manager of any other employee?
A. SELECT e.last_name FROM employees e WHERE NOT EXISTS (SELECT 1 FROM employees m WHERE m.manager_id = e.emp_id);Correct answer
The correlated subquery is re-evaluated per outer row and asks only whether any row exists whose manager_id equals this employee's emp_id. NOT EXISTS is TRUE exactly when that probe returns zero rows, and rows with a NULL manager_id simply fail the correlation predicate rather than poisoning the result — so NULLs cannot suppress the whole answer the way NOT IN does.
B. SELECT e.last_name FROM employees e WHERE e.emp_id NOT IN (SELECT m.manager_id FROM employees m);
Treats NOT IN as safe when the subquery can return NULL. Because at least one employee has a NULL manager_id, the subquery result contains NULL, so `emp_id NOT IN (..., NULL)` evaluates to UNKNOWN for every candidate row that is not an outright match and to FALSE for those that are — the query returns no rows at all.
C. SELECT e.last_name FROM employees e WHERE e.manager_id IS NULL;
Confuses the two directions of the self-reference: it tests whether the employee HAS a manager, not whether the employee IS a manager. It returns the top-of-hierarchy employees, which is a different set from the employees nobody reports to.
D. SELECT DISTINCT m.last_name FROM employees m JOIN employees e ON e.manager_id = m.emp_id;
Inverts the requirement: the join keeps only the parent rows that are matched by some child, so it lists the employees who DO manage someone — the exact complement of the requested set.
Explanation
NOT EXISTS and NOT IN are not interchangeable when the inner query can produce NULL. `x NOT IN (subquery)` is rewritten as `x <> v1 AND x <> v2 AND ...`; a NULL member makes one conjunct UNKNOWN, so the predicate can never be TRUE and the outer query returns nothing. A correlated NOT EXISTS tests only whether the probe returned any row, so a NULL correlation column merely fails to match and the anti-join behaves as intended. Reversing the correlation (matching the employee's own manager_id, or joining parent to child) answers a different question entirely.