The set operators UNION, UNION ALL, INTERSECT, and MINUS can manipulate the result sets of two queries that specify the same number of columns in the Projection clause, and that have compatible data types in the corresponding columns of both queries. Show (The MINUS set operator has EXCEPT as its keyword synonym. Results that the MINUS and EXCEPT operators return from the same operands are always identical.) These operators perform basic set operations of union, intersection, and difference on the result sets of two queries that are the left and right operands of the set operators:
The set operators are useful in business analytic contexts. They can also be used in SELECT statements to check the integrity of your database after you have performed a DML operation like UPDATE, INSERT, DELETE, or MERGE. The set operators can similarly be used when you transfer data to a history table, for example, when you need to verify that the correct data is in the history table before you delete rows from the original table. All of the set operators have the same precedence. In complex queries that include more than one set operator, the precedence of operators is from left to right. Use parentheses to group set operators and their operands, if you need to override the default left-to-right precedence of set operators. Only the UNION set operator supports the ALL keyword. The ALL keyword is not valid with the INTERSECT, MINUS, or EXCEPT set operators, from which only distinct rows are returned. When comparing rows to calculate a set intersection or difference, two NULL values are considered equal in INTERSECT and MINUS operations. The set operators UNION and UNION ALL can combine the result sets of two queries that specify the same number of columns in the Projection clause, and that have compatible data types in the corresponding columns of both queries.The set operators UNION, UNION ALL, INTERSECT, and MINUS can manipulate the result sets of two queries that specify the same number of columns in the Projection clause, and that have compatible data types in the corresponding columns of both queries.
(The MINUS set operator has EXCEPT as its keyword synonym. Results that the MINUS and EXCEPT operators return from the same operands are always identical.) These operators perform basic set operations of union, intersection, and difference on the result sets of two queries that are the left and right operands of the set operators:
The set operators are useful in business analytic contexts. They can also be used in SELECT statements to check the integrity of your database after you have performed a DML operation like UPDATE, INSERT, DELETE, or MERGE. The set operators can similarly be used when you transfer data to a history table, for example, when you need to verify that the correct data is in the history table before you delete rows from the original table. All of the set operators have the same precedence. In complex queries that include more than one set operator, the precedence of operators is from left to right. Use parentheses to group set operators and their operands, if you need to override the default left-to-right precedence of set operators. Only the UNION set operator supports the ALL keyword. The ALL keyword is not valid with the INTERSECT, MINUS, or EXCEPT set operators, from which only distinct rows are returned. When comparing rows to calculate a set intersection or difference, two NULL values are considered equal in INTERSECT and MINUS operations. Page 2
This chapter describes the syntax and semantics of SQL statements that are recognized by HCL OneDB™. The SQL statement names that appear as the title to each statement description in this chapter are listed in alphabetical order. For some statements, important details of the semantics appear in other volumes of this documentation set, as indicated by cross-references. For many statements, the syntax diagram, or the table of terms immediately following the diagram, or both, includes references to syntax segments in Data types and expressions or in Other syntax segments. When the name of an SQL statement includes lowercase characters, such as SET Database Object Mode, it means that the first mixed-lettercase string in the statement name is not an SQL keyword, but that two or more different SQL keywords can follow the preceding uppercase keyword. For an explanation of the structure of statement descriptions, see Overview of SQL syntax. Page 3
The HCL OneDB™ Guide to SQL: Syntax describes the syntax of the statements, data types, expressions, operators, and built-in functions of the HCL OneDB dialect of the SQL language. This information is intended for the following users:
This information assumes that you have the following background:
Page 4
You can use the HCL OneDB™ implementation of the SQL language to develop applications for HCL OneDB database servers.
This article will provide a deep dive into the SQL Union operator, describing its many uses along with examples and explore some common questions like the differences between Union vs Union All. To address real-world data requirements, we may need to combine result sets from multiple data sources so that we could do data analysis or create new datasets. The datasets may be identical but there are chances that they reference different tables. Is there a way to combine the data in a single query? Are Set Operators a viable option? Let’s get started and see how some of the existing operators can be used to help us address these common challenges. In this article, we’ll review:
OperatorsAn operator is a symbol or a keyword defines an action that is performed on one or more expressions in the Select statement. Set OperatorLet’s get into the details of Set Operators in SQL Server, and how to use them There are four basic Set Operators in SQL Server:
UnionThe Union operator combines the results of two or more queries into a distinct single result set that includes all the rows that belong to all queries in the Union. In this operation, it combines two more queries and removes the duplicates.
For example, the table ‘A’ has 1,2, and 3 and the table ‘B’ has 3,4,5.
The SQL equivalent of the above data is given below
In the output, you can see a distinct list of the records from the two result sets
Union AllWhen looking at Union vs Union All we find they are quite similar, but they have some important differences from a performance results perspective. The Union operator combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the Union. In simple terms, it combines the two or more row sets and keeps duplicates.
For example, the table ‘A’ has 1,2, and 3 and the table ‘B’ has 3,4,5.
The SQL equivalent of the above data is given below
In the output, you can see all the rows that include repeating records as well.
INTERSECTThe interest operator keeps the rows that are common to all the queries
For the same dataset from the aforementioned example, the intersect operator output is given below
The SQL Representation of the above tables
The row ‘3’ is common between the two result sets.
EXCEPTThe EXCEPT operator lists the rows in the first that are not in the second.
For the same dataset from the aforementioned example, the Except operator output is given below
The SQL representation of the above tables with EXCEPT operator is given below
List the non-common rows from the first set.
Note: It is very easy to visualize a set operator using a Venn diagram, where each of the tables is represented by intersecting shapes. The intersections of the shapes, where the tables overlap, are the rows where a condition is met. Syntax:The syntax for the Union vs Union All operators in SQL is as follows: SELECT Column1, Column2, … ColumnN FROM <table> [WHERE conditions] [GROUP BY Column(s]] [HAVING condition(s)] UNION SELECT Column1, Column2, … ColumnN FROM table [WHERE condition(s)]; ORDER BY Column1,Column2… |