The used SELECT statements have a different number of columns
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.
- 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
UNION query with column count mismatch between branches.
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.
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.
✕ Use SELECT * in UNION branches
Schema changes in any table will silently change the column count and break the UNION.
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