Sobes.tech
Junior — Middle

What is the main purpose of the UNION operation in SQL and how does it differ from similar operators?

sobes.tech AI

Answer from AI

The UNION operation in SQL is used to combine the results of two or more SELECT queries into a single dataset. Its main purpose is to obtain unique rows from all queries, excluding duplicates.

Differences from similar operators:

  • UNION ALL — combines the results of all queries, including duplicates.
  • INTERSECT — returns only those rows that are present in all queries.
  • EXCEPT (or MINUS in some DBMS) — returns rows from the first query that are not in the second.

Example:

SELECT name FROM employees
UNION
SELECT name FROM managers;

This query will return a list of unique names from the employees and managers tables.

What is the main purpose of the UNION operation in… - sobes.tech