1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. What is the difference between UNION and UNION ALL in SQL?

Added 30 Nov -0001

What is the difference between UNION and UNION ALL in SQL?

  1. UNION sorts the output; UNION ALL reverses the sort
  2. UNION removes duplicate rows; UNION ALL keeps every row Correct answer
  3. UNION keeps duplicates; UNION ALL removes them
  4. UNION works on columns; UNION ALL only on single values

Correct answer

B. UNION removes duplicate rows; UNION ALL keeps every row

18 views

MCQSEXAM

Explanation

UNION merges result sets and then eliminates duplicate rows, which requires a sort or hash step and therefore extra cost. UNION ALL simply concatenates the rows and keeps duplicates, making it considerably faster when duplicates are impossible or acceptable. Both require the same number of columns with compatible types, and neither guarantees result order without an explicit ORDER BY.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs