## 10.5. `UNION`, `CASE`, and Related Constructs

[]()[]()[]()[]()[]()[]()

SQL `UNION` constructs must match up possibly dissimilar
types to become a single result set. The resolution algorithm is
applied separately to each output column of a union query. The`INTERSECT` and `EXCEPT` constructs resolve
dissimilar types in the same way as `UNION`.
Some other constructs, including`CASE`, `ARRAY`, `VALUES`,
and the `GREATEST` and `LEAST`functions, use the identical
algorithm to match up their component expressions and select a result
data type.

**Type Resolution for `UNION`, `CASE`,
and Related Constructs**

1. If all inputs are of the same type, and it is not `unknown`,
   resolve as that type.

2. If any input is of a domain type, treat it as being of the
   domain's base type for all subsequent steps. [<sup class="footnote" id="id-1.5.9.10.9.3.1.1">[12]</sup>](#ftn.id-1.5.9.10.9.3.1.1)

3. If all inputs are of type `unknown`, resolve as type`text` (the preferred type of the string category).
   Otherwise, `unknown` inputs are ignored for the purposes
   of the remaining rules.

4. If the non-unknown inputs are not all of the same type category, fail.

5. Select the first non-unknown input type as the candidate type,
   then consider each other non-unknown input type, left to right. [<sup class="footnote" id="id-1.5.9.10.9.6.1.1">[13]</sup>](#ftn.id-1.5.9.10.9.6.1.1)If the candidate type can be implicitly converted to the other type,
   but not vice-versa, select the other type as the new candidate type.
   Then continue considering the remaining inputs. If, at any stage of this
   process, a preferred type is selected, stop considering additional
   inputs.

6. Convert all inputs to the final candidate type. Fail if there is not an
   implicit conversion from a given input type to the candidate type.

Some examples follow.

**Example 10.10. Type Resolution with Underspecified Types in a Union**

```
SELECT text 'a' AS "text" UNION SELECT 'b';

 text
