Question 1
The employees table contains the following rows (only the relevant columns are shown): | dept_id | manager_id | |---------|------------| | 10 | NULL | | 20 | 100 | | 20 | 100 | | 20 | 101 | | 30 | 106 | | 30 | 106 | | 30 | NULL | | 10 | 100 | How many rows does the following query return? ```sql SELECT DISTINCT dept_id, manager_id FROM employees; ```
A. 8
Assumes DISTINCT evaluates uniqueness over all columns in the underlying table row — including emp_id — rather than only the columns named in the select list. Because every employee has a unique emp_id, no row would be eliminated under that assumption. DISTINCT operates strictly on the projected columns (dept_id and manager_id here), not on the full underlying row.
B. 3
Counts only the unique dept_id values (10, 20, 30) as if DISTINCT scoped to the first selected column alone. DISTINCT is a qualifier on the entire select list, so both dept_id and manager_id are evaluated together when testing for duplicate rows.
C. 5
Treats any two rows sharing a NULL manager_id as a single duplicate regardless of their dept_id — as if NULL collapsed globally across the column. Two rows are duplicates under DISTINCT only when every selected column matches; (10, NULL) and (30, NULL) differ on dept_id, so they remain two distinct rows.
D. 6Correct answer
DISTINCT compares the combination of all selected column values. From the eight rows, (20, 100) appears for two employees and (30, 106) appears for two employees; removing one copy of each leaves six unique pairs: (10, NULL), (20, 100), (20, 101), (30, 106), (30, NULL), and (10, 100). Oracle treats two NULL values as equivalent for DISTINCT, but the two NULL-manager rows differ on dept_id so neither is eliminated.
Explanation
DISTINCT de-duplicates on the combination of every expression in the select list, not just the leftmost column. Two rows are considered duplicates only when all their projected column values match — including when both columns hold NULL, which Oracle treats as equivalent for this purpose. Among the eight employee rows, the pair (20, 100) appears twice and the pair (30, 106) appears twice; each duplicate is eliminated once, leaving six unique (dept_id, manager_id) combinations.