Table A(x) has rows 1, 2, 2, 3. Table B(x) has rows 2, 3, 4. SELECT COUNT(*) FROM (SELECT x FROM A UNION SELECT x FROM B) t; What is returned?
Correct answer A. 4
Explanation
UNION removes duplicate rows from the combined result, including duplicates that come from the same table. The distinct values across both tables are 1, 2, 3 and 4, so the count is 4. The answer 7 is what UNION ALL would give, because it keeps every row from both inputs (4 + 3). UNION ALL is also faster because it skips the duplicate-elimination step.
Answer checked How we write and check questions
Discussion