SQLCODE -122: Column or expression in SELECT list not valid
SQLCODE -122 (SQLSTATE 42803) means the SELECT list, or a sort key in ORDER BY, mixes grouped and ungrouped data. A column that isn't in GROUP BY appears outside an aggregate function in a grouped query.
Other names for this code
Also written as SQL -122, SQLCODE=-122, SQLCODE = -122, db2 -122, sqlstate 42803, SQL0122N, column or expression in the select list is not valid.
What it means
When a query uses GROUP BY, or uses an aggregate function such as SUM or COUNT, each row of the result stands for a group of rows. Every value in the SELECT list must then have one value per group. Grouping columns and expressions do. Aggregate functions do. A plain column that isn't grouped doesn't, because it can differ from row to row inside the group, so Db2 rejects the statement with -122.
The Db2 for z/OS documentation lists four cases. A SELECT list has a column and an aggregate function but no GROUP BY. A column, possibly inside a scalar function, is in the SELECT list but not in GROUP BY. An expression in the SELECT list isn't in GROUP BY. Or the result is grouped and an ORDER BY sort key mixes grouped and ungrouped data. A HAVING clause with no GROUP BY makes the whole table one group, so every column in the SELECT list must then be inside an aggregate function.
An expression only counts as grouped if it matches a GROUP BY expression exactly, ignoring blanks.
Common causes
- A column added to the SELECT list of a grouped query and not to GROUP BY.
- A detail column next to a total, such as
SELECT EMPNO, SUM(SALARY) FROM EMPwith no GROUP BY. - Grouping by an expression such as
SUBSTR(DEPT, 1, 2)and selecting DEPT, or a different expression, on its own. - An ORDER BY key on an ungrouped column in a grouped query.
- A HAVING clause without GROUP BY in a query that also selects plain columns.
How to find the problem
- Take every item in the SELECT list and the ORDER BY list. Each must be in GROUP BY, built only from grouping columns, or inside an aggregate function.
- Compare grouping expressions character for character with GROUP BY, ignoring blanks.
- For static SQL, the bind output gives the statement number. For a query tool, the
DSNT408Imessage appears under the statement.
How to fix it
- Add the column or expression to GROUP BY if each value really should form its own group.
- Wrap the column in an aggregate if one value per group is enough, for example MAX(LASTNAME).
- Remove the column from the SELECT list if it isn't needed.
- To show detail rows next to totals, join the detail to a grouped subquery, or use an OLAP function such as
SUM(SALARY) OVER (PARTITION BY DEPT). - Use the same expression text in SELECT, GROUP BY and ORDER BY.
What it looks like
DSNT408I SQLCODE = -122, ERROR: COLUMN OR EXPRESSION IN THE SELECT LIST IS NOT VALID DSNT418I SQLSTATE = 42803 SQLSTATE RETURN CODE
IBM documentation
IBM documentation: -122. Use the documentation for your installed release.
Related codes
Questions
Why does the query need GROUP BY just because I used SUM?
An aggregate turns many rows into one. Any other column in the SELECT list must also give one value per group, which means it has to be grouped or aggregated.
Why do -119 and -122 have the same SQLSTATE?
They are the same mistake in different clauses. -119 is in HAVING and -122 is in the SELECT list or ORDER BY. Both use SQLSTATE 42803.
Is SQL0122N on Db2 for LUW the same?
Yes. It has SQLSTATE 42803 and covers the same grouping mistakes.