1222ERRORTier 2 — Caution✅ HIGH confidence

The used SELECT statements have a different number of columns

Category: Query SyntaxVersions: All MariaDB / MySQL versions

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
SELECT id, name FROM customers
UNION ALL
SELECT id, name, email FROM prospects;  -- extra column
ERROR 1222 (21000): The used SELECT statements have a different number of columns

Fix 1: Align column counts across all UNION branches

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

fix
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

Why it's wrong: Schema changes in any table will silently change the column count and break the UNION.

Sources

📚 Official docs: https://mariadb.com/kb/en/union/

🔧 Source ref: MariaDB Server error code 1222 / ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT

📖 Further reading: MariaDB UNION

Confidence assessment

✅ HIGH confidence

Stable.

See also

📄 Reference pages

UNIONINTERSECTEXCEPT
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →