# SQLCODE -421: Set operands have different column counts

Source: https://wndsoftware.com/mainframe-errors/sqlcode-421

### SQLCODE -421: Set operands have different column counts

SQLCODE -421 (SQLSTATE 42826) means the queries combined by UNION, UNION ALL, EXCEPT or INTERSECT don't all return the same number of columns. Every operand must produce exactly as many columns as the others.


#### Other names for this code

Also written as `SQL -421`, `SQLCODE=-421`, `SQLCODE = -421`, `db2 -421`, `sqlstate 42826`, `SQL0421N`, `db2 union different number of columns`, `SQL0421`.


### What it means

A set operator stacks the results of two or more queries into one result table. Db2 lines the columns up by position, so the first column of each query becomes the first column of the result, and so on. Column names don't matter for this. The count does. If one query returns five columns and another returns six, there is no way to stack them, and Db2 rejects the statement with -421 when it is bound or prepared.

Db2 for z/OS has supported EXCEPT and INTERSECT since Db2 9, and the rule is the same for all of them. Matching the count isn't enough on its own. The columns in each position must also have compatible data types, and a mismatch there gives a different code, -415.

Db2 for LUW's SQL0421N also covers the rows of a VALUES clause that lists several rows with different numbers of values.


### Common causes

- Someone added a column to one SELECT list in a long UNION and forgot the other branches.
- One operand uses `SELECT *` and the table has gained a column since the SQL was written. Dynamic SQL fails at once. Static SQL fails at the next bind or rebind.
- The operands read different tables with `SELECT *`, and the tables were never the same shape.
- SQL generated by a reporting tool or a program built the branches from different column lists.
- One branch needs a placeholder value for a column that has no equivalent in its table, and the placeholder is missing.


### How to find the problem

- Count the items in each SELECT list. Format the statement with one column per line so the counts are easy to compare.
- Expand any `SELECT *` by counting the table's columns with `SELECT COUNT(*) FROM SYSIBM.SYSCOLUMNS WHERE TBCREATOR = 'PAYROLL' AND TBNAME = 'EMP_HIST'`.
- If the statement is in a view, read its text from SYSIBM.SYSVIEWS.
- With many branches, run them in pairs until you find the one that's out of line.


### How to fix it

- List the columns by name in every branch instead of using `SELECT *`.
- Pad a branch that has no matching column with a typed null or a constant, for example `CAST(NULL AS DATE)` or `0`, so the counts and types line up.
- Keep the branches in the same order, column for column, and comment which output column each one feeds.
- Rebind static packages after the fix, and check them again whenever a table in the UNION is altered.


### IBM documentation

[IBM documentation: -421](https://www.ibm.com/docs/en/db2-for-zos/13.0.0?topic=codes-421). Use the documentation for your installed release.

[Report a correction to WND Software](https://wndsoftware.com/contact#message).

Technical reviewer: [Albert Joseph](https://wndsoftware.com/authors/albert-joseph), WND Software.

Last reviewed 2026-10-18 by the WND Software team. Written in our own words. For the full reference, check IBM’s Db2 for z/OS documentation for your release.


### Related codes

[SQLCODE -206 Column name not valid Read →](https://wndsoftware.com/mainframe-errors/sqlcode-206)
[SQLCODE -104 Illegal symbol in the statement Read →](https://wndsoftware.com/mainframe-errors/sqlcode-104)
[SQLCODE -401 Operands not comparable Read →](https://wndsoftware.com/mainframe-errors/sqlcode-401)
[SQLCODE -208 ORDER BY column not in result table Read →](https://wndsoftware.com/mainframe-errors/sqlcode-208)
[SQLCODE -203 Ambiguous column reference Read →](https://wndsoftware.com/mainframe-errors/sqlcode-203)

### Questions


#### Do the column names have to match across a UNION?

No. Db2 matches columns by position. If the names in one position differ, the result column has no usable name, so give it the same name with AS in every branch if you need to refer to it.


#### The counts match. Why does the UNION still fail?

The data types in each position must also be compatible. A number in one branch and a date in the same position of another gives -415, not -421.


#### Is SQL0421N on Db2 for LUW the same?

Yes, with SQLSTATE 42826. On LUW the same message also covers a multi-row VALUES clause whose rows have different numbers of values.

[Db2 SQLCODEs](https://wndsoftware.com/mainframe-errors/db2-sqlcodes) · [SQLCODE -420](https://wndsoftware.com/mainframe-errors/sqlcode-420) · [SQLCODE -433](https://wndsoftware.com/mainframe-errors/sqlcode-433)

[← All error codes](https://wndsoftware.com/mainframe-errors/)
