- Set operations (SQL)
=UNION operator=
In
SQL theUNION
clause combines the results of two SQL queries into a single table of all matching rows. The two queries must have the same number of columns and compatible data types to unite. Any duplicate records are automatically removed unlessUNION ALL
is used.UNION
can be useful indata warehouse applications where tables aren't perfectly normalized. ["aUNION ALL
views technique for managing maintenance and performance in your large data warehouse environment ... ThisUNION ALL
technique has saved many of my clients with issues related to time-sensitive database designs. These databases usually have an extremely volatile current timeframe, month, or day portion and the older data is rarely updated. Using different container DASD allocations, tablespaces, tables, and index definitions, the settings can be tuned for the specific performance considerations for these different volatility levels and update frequency situations." [http://www.dbazine.com/datawarehouse/dw-articles/beulke4 Terabyte Data Warehouse Table Design Choices - Part 2] (URL accessed onJuly 25 ,2006 )] A simple example would be a database having tablessales2005
andsales2006
that have identical structures but are separated because of performance considerations. AUNION
query could combine results from both tables.Note that
UNION
does not guarantee the order of rows. Rows from the second operand may appear before, after, or mixed with rows from the first operand. In situations where a specific order is desired,ORDER BY
must be used.Note that
UNION ALL
may be much faster than plainUNION
.Examples
Given these two tables:
Note that there are two rows for Joe because those rows are distinct across their columns. There is only one row for Alex because those rows are not distinct for both columns.
UNION ALL
gives different results, because it will not eliminate duplicates. Executing this statement:would give these results, again allowing variance for the lack of an
ORDER BY
statement:The discussion of full outer joins also has an example that uses
UNION
.INTERSECT operator
The SQL
INTERSECT
operator takes the results of two queries and returns only rows that appear in both result sets. For purposes of duplicate removal theINTERSECT
operator does not distinguish betweenNULLs
. TheINTERSECT
operator removes duplicate rows from the final result set. TheINTERSECT ALL
operator does not remove duplicate rows from the final result set.Example
The following example
INTERSECT
query returns all rows from the Order table where Quantity is between 50 and 100.EXCEPT operator
The SQL
EXCEPT
operator takes the distinct rows of one query and returns the rows that do not appear in a second result set. TheEXCEPT ALL
operator does not remove duplicates. For purposes of row elimination and duplicate removal, theEXCEPT
operator does not distinguish betweenNULLs
.Notably, the Oracle platform provides a
MINUS
operator which is functionally equivalent to theEXCEPT DISTINCT
operator [http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/ap_standard_sql003.htm#g14847] .Example
The following example
EXCEPT
query returns all rows from the Order table where Quantity is between 1 and 49, and those with a Quantity between 76 and 100.ee also
*
Union (set theory)
*
*Select (SQL) References
External links
* [http://msdn2.microsoft.com/en-us/library/ms180026.aspx MSDN documentation on UNION in Transact-SQL for SQL Server]
* [http://www.sqlexpert.co.uk/2007/11/order-by-clause-in-statements-with.html Naming of select list items in set operations]
* [http://www.mysqltutorial.org/sql-union-mysql.aspx UNION in MySQL with Examples]
* [http://dev.mysql.com/doc/refman/5.0/en/union.html UNION in MySQL]
* [http://www.postgresql.org/docs/current/static/sql-select.html#SQL-UNION UNION Clause in PostgreSQL]
* [http://www.w3schools.com/sql/sql_union.asp SQL UNION and UNION ALL]
* [http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.dwe.tutorial.doc/tutetlmod1_lesson2.htm Designing a data flow that loads a warehouse table]
* [http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/queries004.htm Oracle 11g documentation for UNION (ALL), INTERSECT and MINUS]
* [http://www.gplivna.eu/papers/sql_set_operators.htm SQL Set Operators]
Wikimedia Foundation. 2010.