What are SQL Unions?

SQL Stands for Structured Query Language(SQL). SQL UNION combines the results of two or more queries into a single result set in a database that includes all the rows that belong to all queries in the union.

SELECT 1 AS [Column1], 2 AS [Column2] UNION SELECT 3 AS [Column1], 4 AS [Column2] 
SQL Union

Figure: Union

Union vs Union All

Union combines two tables by removing duplicates, whereas Union All combines two tables by including all data, even if they are duplicates.

Differences Between UNION, MINUS, and INTERSECT

  • UNION combines the results from 2 tables and eliminates duplicate records from the result set.
  • The MINUS operator, when used between 2 tables, gives us all the rows from the first table except the rows which are present in the second table.
  • INTERSECT operator returns us only the matching or common rows between 2 result sets.