Sobes.tech
Junior — Middle

Mis on UNION operaatori SQL-is ja eesmärk ja millistes olukordades seda kasutatakse?

sobes.tech AI

Vastus AI-lt

SQL operator UNION is used to combine the results of two or more SELECT queries into a single data set. The main goal is to obtain unique rows from all queries.

Features and application:

  • Combines results with the same number and types of columns.
  • Removes duplicates (UNION ALL retains duplicates).
  • Used when data is distributed across different tables or queries, but needs to be presented in a single list.

Example:

SELECT name FROM employees
UNION
SELECT name FROM contractors;

This query will return a list of unique names from both tables.