Sobes.tech
Junior — Middle

For what purposes is the UNION operator used in SQL and how does it affect the query execution result?

sobes.tech AI

Answer from AI

The UNION operator in SQL is used to combine the results of two or more SELECT queries into a single dataset. It returns unique rows from all queries, excluding duplicates.

Main features of UNION:

  • All combined queries must return the same number of columns with compatible data types.
  • The result contains only unique rows (if all rows including duplicates are needed, UNION ALL is used).

Example:

SELECT name FROM employees WHERE department = 'Sales'
UNION
SELECT name FROM contractors WHERE department = 'Sales';

This query will return a list of unique names from two tables related to the sales department.

For what purposes is the UNION operator used in SQL… - sobes.tech