SQL error · Subqueries and set operations

UNION has incompatible types

Problem: The columns in the same position cannot be converted to a common type on your engine.

Solution: Decide what that column means and use CAST to a suitable type in each branch.

Why it happens: UNION has incompatible types

Both branches have the same shape: sort_order and value. The conflict is in the second position, where one branch produces an integer and the other produces text.

PostgreSQL · problem query
SELECT 1 AS sort_order,
       42::integer AS value
UNION ALL
SELECT 2,
       'no-data'::text;

Under PostgreSQL's type resolution rules, integer and text do not share a common category for that column, so the union is rejected. In SQL Server, an equivalent combination may try to convert the text to the higher-precedence type and fail if the value cannot be converted. MySQL, by contrast, documents that SELECT 1 UNION SELECT 'a' is valid and builds a result able to hold both values.

“UNION has incompatible types” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: UNION types integer and text cannot be matched
SQL ServerMsg 245, Level 16: Conversion failed when converting the varchar value 'no-data' to data type int.
OracleORA-01790: expression must have same datatype as corresponding expression

MySQL converts types implicitly and rarely fails (it may raise ERROR 1271 for collations); SQLite accepts different types because its columns are dynamically typed.

How to fix it in SQL

Before adding CAST, ask what that position represents. If value is a label that sometimes holds numbers and sometimes text, converting both branches to text may be right. If it represents an amount, converting 'no-data' to text only hides a data quality problem: you should clean the data or represent it as NULL.

Explicit text contract
SELECT 1 AS sort_order,
       CAST(42 AS VARCHAR(20)) AS value
UNION ALL
SELECT 2,
       CAST('no-data' AS VARCHAR(20));

An explicit conversion documents the intent and reduces differences between engines. Even so, the chosen type must reflect what the data really is; do not use text as a universal output just to silence errors.

How to check it

Test each conversion with real values, including NULLs, invalid text and length limits.

How to avoid it next time

Converting everything to text can change later sorting and calculations.

Related cases worth checking

The same SQL can behave differently depending on the engine

EngineWhat to checkPractical consequence
PostgreSQLResolves one common type per column; if the categories are not compatible or there is no implicit conversion, it fails.Declare the target type with CAST when the meaning really is shared.
SQL ServerThe columns must be compatible through implicit conversion, and type precedence applies.Text that cannot be converted may raise an error when the engine tries to turn it into a higher-precedence type.
MySQLThe matching types should agree, but the final type may be widened or adapted to fit all branches.A query that is accepted here may not be portable to engines with stricter resolution.
SQLiteThe type belongs to the value, and the engine uses dynamic typing.It can keep values of different classes in the same column of a compound query.

Local check: SQLite accepts mixed values

The query below was run locally with SQLite during this review. The result shows the same column holding an integer in one row and text in another.

SQLite · permitted behaviour
WITH combined AS (
  SELECT 1 AS sort_order, 42 AS value
  UNION ALL
  SELECT 2, 'no-data'
)
SELECT sort_order,
       value,
       typeof(value) AS type
FROM combined
ORDER BY sort_order;
Local result in SQLite
sort_ordervaluetype
142integer
2no-datatext

This result does not contradict PostgreSQL or SQL Server: SQLite has a different type system. If your query has to work across engines, set the output type deliberately instead of relying on implicit coercion.