1222
MariaDBERRORNotableQuery SyntaxHIGH confidence

The used SELECT statements have a different number of columns

What this means

Error 1222 (SQLSTATE 21000) is raised when the SELECT statements in a UNION, UNION ALL, INTERSECT, or EXCEPT have different numbers of columns. All branches of a set operation must return the same number of columns.

Why it happens
  1. 1A column was added to one branch of a UNION but not the others
  2. 2SELECT * used in UNION branches that have different table schemas
  3. 3A subquery is expected to return a single column but returns multiple
How to reproduce

UNION query with column count mismatch between branches.

trigger — this will error
trigger — this will error
SELECT id, name FROM customers
UNION ALL
SELECT id, name, email FROM prospects;  -- extra column

expected output

ERROR 1222 (21000): The used SELECT statements have a different number of columns

Fix

Align column counts across all UNION branches

WHEN Always — each SELECT in a UNION must return the same number of columns.

Align column counts across all UNION branches
SELECT id, name, NULL AS email FROM customers
UNION ALL
SELECT id, name, email FROM prospects;

Why this works

Pad missing columns with NULL (or a literal) to match the column count of the branch with the most columns. Column names are taken from the first SELECT.

What not to do

Use SELECT * in UNION branches

Schema changes in any table will silently change the column count and break the UNION.

Sources
Official documentation ↗

MariaDB Server error code 1222 / ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT

MariaDB UNION

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors