UNION allows you to combine two or more result sets into a single result set. There are a few rules that need to be followed to avoid errors, listed as follows:
- The number of columns in the SELECT statements must be the same.
- The order of the columns in the SELECT statements must be the same.
- The data types of the columns must be the same or of a compatible type.
- The ORDERBY clause can only be used on the final SELECT statement.
There are also a couple of things to keep in mind when reviewing your results, as follows:
- The names of the final columns are generated from the column names you use in the first SELECT statement.
- GROUP BY and HAVING clauses can only ...