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

What is the difference among ...

Câu trả lời

The SQL set operators UNION, MINUS, and INTERSECT are used to combine the results of two or more SELECT statements. Each operator has a distinct purpose and behavior:

UNION

  • Purpose: Combines the results of two or more SELECT statements into a single result set.
  • Behavior:
    • UNION removes duplicate rows from the result set.
    • UNION ALL includes all rows from the combined SELECT statements, including duplicates.
  • Usage: Useful when you need to merge results from different queries and ensure that the final result set contains unique rows.
  • Example:
    sql Copy
    SELECT column_name FROM table1
    UNION
    SELECT column_name FROM table2;

MINUS (or EXCEPT)

  • Purpose: Returns the rows from the first SELECT statement that are not present in the second SELECT statement.
  • Behavior:
    • Removes duplicates from the result set.
    • The result set includes only the rows that are unique to the first SELECT statement.
  • Usage: Useful when you need to find differences between two result sets.
  • Example:
    sql Copy
    SELECT column_name FROM table1
    MINUS
    SELECT column_name FROM table2;

INTERSECT

  • Purpose: Returns only the rows that are present in both SELECT statements.
  • Behavior:
    • Removes duplicates from the result set.
    • The result set includes only the rows that are common to both SELECT statements.
  • Usage: Useful when you need to find common elements between two result sets.
  • Example:
    sql Copy
    SELECT column_name FROM table1
    INTERSECT
    SELECT column_name FROM table2;

Key Points

  • Column Compatibility: For all these operators, the number of columns and their data types in the SELECT statements must match.
  • Performance Considerations: ...
senior

senior

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

middle

What is the difference between INNER JOIN , OUTER JOIN , FULL OUTER JOIN ?

middle

How does a Hash index work?

middle

What is Collation?

Bình luận

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

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