The SQL UNION operator
The UNION operator is used to combine the result sets of two or more SELECT statements.
Note that the SELECT statement within the UNION must have the same number of columns. Columns must also have similar data types. Also, the order of the columns in each SELECT statement must be the same.
The UNION SQL grammar
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
Note: By default, the UNION operator picks a different value. If duplicate values are allowed, use UNION ALL.
SQL UNION ALL syntax
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
Also, the column name in the UNION result set is always equal to the column name in the first SELECT statement in the UNION.
Error reporting and resolution
When using the UNION operator, the following error can easily be reported:
Parse error: org. Apache. Hadoop. Hive. Ql. Parse. ParseException: line 5-0 always recognize input near “and” (” and “the UNION, ‘ ‘SELECT’ set in the operator
Simplify the code used as follows:
SELECT
device_id
FROM
tableA
UNION
(SELECT
device_id
FROM
tableB as a1
INNER JOIN tableC as a2
on a1.device_id = a2.device_id
)
This is mainly because the UNION operator can only join fields to fields, but not fields to tables or tables, even if the number of fields and their names and formats are the same.
Therefore, you can’t have parentheses around the UNION operator, because it’s easy for SQL to determine that this is a subquery/table and therefore to report an error when joining
The solution
- > SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>> SELECT FROM TABLE D OL>
Matters needing attention
- > Multiple UNION operators can be used in SQL statements. SELECT statements that use the UNION operator cannot contain their own ORDER BY or COMPUTE clauses. You can only use an ORDER BY or COMPUTE clause on the final combined result set (that is, after the last SELECT statement). The GROUP BY and HAVING clauses can be used in SELECT statements that use the UNION operator. By default, SQL Server 2005 evaluates statements containing the UNION operator from left to right. You can specify the order of evaluation using parentheses (I haven’t tested this thoroughly on any other platform, so avoid parentheses altogether, so if you need to specify the order of evaluation, you can simply write that part of the evaluation at the beginning of your SQL statement).
Read More:
- The usage of typing.union in Python
- VNC logon when Connection union (10061) resolves
- On fileZilla unable to connect FATAL ERROR: Connection union
- C++:error C2228: left of ‘.str’ must have class/struct/union
- Common errors and solutions in MapReduce stage
- Python common error: if using all scalar values, you must pass an index (four solutions)
- Common errors and solutions of Qt development application under Ubuntu
- Common solutions to errors in checkout or commit of SVN
- Hive SQL syntax error and corresponding solutions
- R-common errors and their possible causes — Notes
- OpenGL Usage Summary (including problems encountered and solutions)
- Usage of NVL in SQL
- Common problems and solutions under Linux
- Common mistakes and solutions of cmake + QT + VTK
- Linux common problems and Solutions
- Common errors and modification methods of findbug
- Summary of common errors in angularjs
- Analysis and solution of common errors in ArcEngine development
- Common faults and solutions of sylixos IDE (1)