1222ERRORTier 2 — Caution✅ HIGH confidenceThe 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
- 1A column was added to one branch of a UNION but not the others
- 2SELECT * used in UNION branches that have different table schemas
- 3A subquery is expected to return a single column but returns multiple
How to reproduce
UNION query with column count mismatch between branches.
SELECT id, name FROM customers
UNION ALL
SELECT id, name, email FROM prospects; -- extra columnFix 1: Align column counts across all UNION branches
Always — each SELECT in a UNION must return the same number of columns.
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
🔗 Related errors
📄 Reference pages