SQL error · Subqueries and set operations

UNION has a different number of columns

Problem: The SELECT statements do not return the same number of columns.

Solution: Decide what each position represents and line up the column lists in both queries.

Why it happens: UNION has a different number of columns

Problem query
SELECT id, name
FROM customers
UNION ALL
SELECT id, name, city
FROM suppliers;

The first query returns two columns and the second returns three. The rows from customers have no third position to combine with city.

Check the result with small data

The two queries select exactly two columns in the same order.

Self-contained query · SQLite
WITH students(id, name) AS (VALUES (1,'Amy')),
     teachers(id, name) AS (VALUES (2,'Liam'))
SELECT id, name FROM students
UNION ALL SELECT id, name FROM teachers;
Query result
idname
1Amy
2Liam

UNION ALL can combine them because the structure matches. If the second query returned only name, the operation would be invalid.

Check your understanding: What would you add to a branch that lacks an optional column?

Show answer

A NULL column with the alias for that position, for example NULL AS city, so that both branches have the same columns.

“UNION has a different number of columns” error messages by engine

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

EngineTypical message
PostgreSQLERROR: each UNION query must have the same number of columns
MySQLERROR 1222 (21000): The used SELECT statements have a different number of columns
SQL ServerMsg 205, Level 16: All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
OracleORA-01789: query block has incorrect number of result columns
SQLiteError: SELECTs to the left and right of UNION do not have the same number of result columns

How to fix it in SQL

Do not add any old column just to make the count match. Decide what each position means in the final result. If you want one list of entities with where each came from, both branches can return id, name and a source label:

Fixed query
SELECT id, name, 'customer' AS source
FROM customers
UNION ALL
SELECT id, name, 'supplier' AS source
FROM suppliers;
Shared shape of the result
idnamesource
1Amycustomer
8Delta SAsupplier

How to check it

Run each branch on its own and compare the number, order, meaning and type of its columns.

How to avoid it next time

Avoid SELECT * when the combined result needs a stable structure.

Related cases worth checking

Having three columns on each side does not, on its own, make a well-designed union. The first column is combined with the first, the second with the second, and so on. PostgreSQL requires compatible types; MySQL works out the result type from the values in the matching columns. Do not mix, say, a date with an identifier just because they sit in the same position.

The visible names in the result are not matched by name either. In engines such as PostgreSQL and MySQL, the output names come from the first branch; what matters for the combination is position and compatibility.