- Source: Set operations (SQL)
Set operations in SQL is a type of operations which allow the results of multiple queries to be combined into a single result set.
Set operators in SQL include UNION, INTERSECT, and EXCEPT, which mathematically correspond to the concepts of union, intersection and set difference.
UNION operator
In SQL the UNION clause combines the results of two SQL queries into a single table of all matching rows. The two queries must result in the same number of columns and compatible data types in order to unite. Any duplicate records are automatically removed unless UNION ALL is used.
UNION can be useful in data warehouse applications where tables are not perfectly normalized. A simple example would be a database having tables sales2005 and sales2006 that have identical structures but are separated because of performance considerations. A UNION query could combine results from both tables.
Note that UNION ALL 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 plain UNION.
= Examples
=Given these two tables:
Executing this statement:
yields this result set, though the order of the rows can vary because no ORDER BY clause was supplied:
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 the INTERSECT operator does not distinguish between NULLs. The INTERSECT operator removes duplicate rows from the final result set. The INTERSECT ALL operator does not remove duplicate rows from the final result set, but if a row appears X times in the first query and Y times in the second, it will appear
min
(
X
,
Y
)
{\displaystyle \min(X,Y)}
times in the result set.
= Example
=The following example INTERSECT query returns all rows from the Orders 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. For purposes of row elimination and duplicate removal, the EXCEPT operator does not distinguish between NULLs. The EXCEPT ALL operator does not remove duplicates, but if a row appears X times in the first query and Y times in the second, it will appear
max
(
X
−
Y
,
0
)
{\displaystyle \max(X-Y,0)}
times in the result set.
Notably, the Oracle platform provides a MINUS operator which is functionally equivalent to the SQL standard EXCEPT DISTINCT operator.
= Example
=The following example EXCEPT query returns all rows from the Orders table where Quantity is between 1 and 49, and those with a Quantity between 76 and 100.
Worded another way; the query returns all rows where the Quantity is between 1 and 100, apart from rows where the quantity is between 50 and 75.
= Example
=The following example is equivalent to the above example but without using the EXCEPT operator.
See also
Union (set theory)
Join (SQL)
SQL:2003
Select (SQL)
References
External links
MSDN documentation on UNION in Transact-SQL for SQL Server
Naming of select list items in set operations
UNION in MySQL with Examples
UNION in MySQL
UNION Clause in PostgreSQL
SQL UNION and UNION ALL
Sort order within UNION statement
Designing a data flow that loads a warehouse table
Oracle 11g documentation for UNION (ALL), INTERSECT and MINUS
SQL Set Operators
Kata Kunci Pencarian:
- X (media sosial)
- Set operations (SQL)
- Set operation
- Join (SQL)
- SQL-92
- Null (SQL)
- SQL syntax
- Microsoft SQL Server
- Merge (SQL)
- View (SQL)
- NoSQL