Câu hỏi phỏng vấn SQL
Câu hỏi

What is the difference between UNION and UNION ALL ?

Câu trả lời

The difference between UNION and UNION ALL in SQL primarily revolves around how they handle duplicate rows when combining the results of multiple SELECT statements.

UNION

  • Deduplication: The UNION operator removes duplicate rows from the combined result set. This means that if the same row appears in the results of multiple SELECT statements, it will only appear once in the final result set.
  • Performance: Because UNION performs a deduplication step, it is generally slower than UNION ALL, especially when dealing with large datasets. The process of checking for and removing duplicates adds overhead.
  • Use Case: UNION is useful when you need to ensure that the final result set contains only unique rows. This is important in scenarios where duplicate data could lead to incorrect results or misinterpretations.

UNION ALL

  • No Deduplication: The UNION ALL operator retains all rows from the combined result set, including duplicates. It simply concatenates the results of the SELECT statements without performing any additional checks.
  • Performance: UNION ALL is faster than UNION because it does not perform the deduplication step. This makes it more efficient for large datasets or when you are certain that there are no duplicates in the data.
  • Use Case: UNION ALL is useful when you need to retain all occurrences of rows, including duplicates. This can be important for tasks like generating reports where the presence of duplicates is meaningful or when you are certain that the datasets being combined do not contain duplicates.

Example

Consider two tables, table1 and table2, with the following data:

table1:
| id | name |
|--...

middle

middle

Gợi ý câu hỏi phỏng vấn

entry

Define a Temp Table

entry

What is a VIEW ?

entry

What is PRIMARY KEY ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào