MySQL 8.4Lab-verified · 2026-11-29

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

The SELECTs joined by UNION or UNION ALL do not return the same number of columns.

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

Jump to a cause

  1. 01UNION of SELECTs with 3 and 2 columns
  2. 02SELECT * UNION ALL after a column was added to one table

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4

Every SELECT in a UNION or UNION ALL must return the same number of columns. If one returns 3 and another 2, MySQL stops with ERROR 1222 before running anything.

Count the columns in each SELECT. With SELECT * the count depends on the current table definition, so a later ALTER TABLE can break a query that used to work.

01UNION of SELECTs with 3 and 2 columns

SELECT id, name, email FROM customers UNION SELECT id, name FROM suppliers failed with ERROR 1222 (21000): The used SELECT statements have a different number of columns. information_schema showed customers with 3 columns and suppliers with 2.

Adding NULL as the third column of the second SELECT (SELECT id, name, NULL FROM suppliers) made the UNION work. Acme came back with email NULL.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(20), email VARCHAR(40));
CREATE TABLE suppliers (id INT PRIMARY KEY, name VARCHAR(20));
INSERT INTO customers VALUES (1, 'Kim', 'kim@example.com');
INSERT INTO suppliers VALUES (7, 'Acme');
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)
Reproducerun as lab_a
SELECT id, name, email FROM customers
UNION
SELECT id, name FROM suppliers;
✕ ERROR
ERROR 1222 (21000): The used SELECT statements have a different number of columns
Checkrun as lab_a
SELECT table_name, COUNT(*) AS columns FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name IN ('customers', 'suppliers')
GROUP BY table_name ORDER BY table_name;
OUTPUT
+------------+---------+
| TABLE_NAME | columns |
+------------+---------+
| customers  |       3 |
| suppliers  |       2 |
+------------+---------+
2 rows in set (0.00 sec)
After the fixrun as lab_a
SELECT id, name, email FROM customers
UNION
SELECT id, name, NULL FROM suppliers;
✓ SUCCESS
+----+------+-----------------+
| id | name | email           |
+----+------+-----------------+
|  1 | Kim  | kim@example.com |
|  7 | Acme | NULL            |
+----+------+-----------------+
2 rows in set (0.01 sec)

02SELECT * UNION ALL after a column was added to one table

orders and orders_2023 started with the same columns, then ALTER TABLE orders ADD COLUMN status added a third. SELECT * FROM orders_2023 UNION ALL SELECT * FROM orders then failed with ERROR 1222.

SHOW COLUMNS FROM orders listed id, amount, status. Listing the columns (SELECT id, amount FROM ... UNION ALL SELECT id, amount FROM ...) returned both rows.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, amount INT);
CREATE TABLE orders_2023 (id INT PRIMARY KEY, amount INT);
INSERT INTO orders VALUES (2, 300);
INSERT INTO orders_2023 VALUES (1, 100);
ALTER TABLE orders ADD COLUMN status VARCHAR(10) DEFAULT 'NEW';
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT * FROM orders_2023
UNION ALL
SELECT * FROM orders;
✕ ERROR
ERROR 1222 (21000): The used SELECT statements have a different number of columns
Checkrun as lab_a
SHOW COLUMNS FROM orders;
OUTPUT
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int         | NO   | PRI | NULL    |       |
| amount | int         | YES  |     | NULL    |       |
| status | varchar(10) | YES  |     | NEW     |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
After the fixrun as lab_a
SELECT id, amount FROM orders_2023
UNION ALL
SELECT id, amount FROM orders
ORDER BY id;
✓ SUCCESS
+----+--------+
| id | amount |
+----+--------+
|  1 |    100 |
|  2 |    300 |
+----+--------+
2 rows in set (0.00 sec)

FAQ

How do I UNION tables with different numbers of columns?

Fill the missing column with a constant such as NULL in the shorter SELECT. In the run above SELECT id, name, NULL FROM suppliers fixed it.

My UNION query worked before and now gives 1222. Why?

If it uses SELECT *, a column added to one table changes the count. That is what happened after ALTER TABLE ADD COLUMN above.

Do the column names have to match in a UNION?

The count must match. In the run above the third column was email in one SELECT and NULL in the other, and the result used the first SELECT's name, email.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13