Question 1
A developer needs a query that returns the `emp_id` of every employee who is also recorded as the `manager_id` of at least one other employee. Which query correctly returns this set?
A. SELECT manager_id FROM employees WHERE manager_id IS NOT NULL MINUS SELECT emp_id FROM employees
The MINUS operands are reversed. This query returns manager_id values not present as any emp_id. Because every manager in this dataset is also an employee, the subtracted set contains all manager IDs and the result is empty—the exact opposite of what is required.
B. SELECT emp_id FROM employees UNION SELECT manager_id FROM employees WHERE manager_id IS NOT NULL
UNION returns every distinct value that appears in either result set—the union, not the intersection. Because every manager in this dataset is also an employee, combining all emp_id values with all non-NULL manager_id values simply reproduces all eight employee IDs, not only the three who manage others.
C. SELECT emp_id FROM employees INTERSECT SELECT manager_id FROM employeesCorrect answer
INTERSECT returns only distinct rows common to both result sets. NULL values in manager_id find no match on the left side because emp_id is a PRIMARY KEY and is never NULL. The remaining manager_id values—100, 101, and 106—each appear in the emp_id list, so exactly those three values are returned.
D. SELECT emp_id FROM employees MINUS SELECT manager_id FROM employees WHERE manager_id IS NOT NULL
MINUS returns rows present in the first result set that are absent from the second—the logical complement of what is needed. Subtracting the manager ID set {100, 101, 106} from all emp_id values yields the employees who are NOT managers: {102, 103, 104, 105, 107}.
Explanation
INTERSECT is the correct operator when the goal is values that appear in both result sets. MINUS returns values present in the first set but absent from the second, which inverts the intended logic; reversing its operands yields an empty set instead. UNION combines both sets (eliminating duplicates), so it returns all employee IDs when every manager is also an employee—far more than the intended subset. NULL values in manager_id are handled without issue because emp_id is a primary key and is never NULL, so no unintended NULL matches can occur.