Practice mode DBMS and SQL

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.