SQLCODE -208: ORDER BY column not in result table
SQLCODE -208 (SQLSTATE 42707) means the ORDER BY clause names a column that isn't part of the result table. It usually happens in a query that combines results with UNION, EXCEPT or INTERSECT.
Other names for this code
Also written as SQL -208, SQLCODE=-208, SQLCODE = -208, db2 -208, sqlstate 42707, SQL0208N, order by clause is invalid because column.
What it means
In a simple SELECT from a table, Db2 can usually sort by a column you didn't select, because it still has the table's rows to work with. A query that combines results with UNION, EXCEPT or INTERSECT is different. Its result is a new table built from several SELECTs, and ORDER BY can only see that result. It can't reach back into the tables the SELECTs read.
So in a UNION, ORDER BY has to name a column of the result, or give a column position such as ORDER BY 2. If it names a column from one of the underlying tables that isn't in the result, Db2 returns -208 and puts the name in the message.
Names add another trap. A result column gets its name from the SELECT lists. If the SELECTs use different names in the same position, the result column has no usable name. Give it the same name with AS in every SELECT.
Common causes
- ORDER BY on a column that is in one of the tables but not in the SELECT lists of a UNION.
- ORDER BY on a column name that is different in each SELECT of a UNION, so the result column has no common name.
- In a UNION, a column renamed with AS in the SELECT lists, with ORDER BY still using the original name.
How to find the problem
- Read the column name in the
DSNT408Imessage or SQLERRMC. - Check whether the query uses UNION, EXCEPT or INTERSECT. If it does, list the result columns and compare their names with the ORDER BY.
- Check whether the column has the same name in every SELECT of the query, or an AS name.
- Run each SELECT on its own to see the result column names it produces.
How to fix it
- Add the column to every SELECT list if you need to sort by it.
- Give the result column the same name with AS in every SELECT of the query, and use that name in ORDER BY.
- Sort by position, as in
ORDER BY 2. It works, but it breaks silently if someone changes the SELECT list later. - Remove table qualifiers from the ORDER BY of a combined query.
- Wrap the UNION in a nested table expression and sort the outer query by its column names.
What it looks like
DSNT408I SQLCODE = -208, ERROR: THE ORDER BY CLAUSE IS INVALID BECAUSE COLUMN HIRE_DATE IS
NOT PART OF THE RESULT TABLE
DSNT418I SQLSTATE = 42707 SQLSTATE RETURN CODEIBM documentation
IBM documentation: -208. Use the documentation for your installed release.
Related codes
Questions
Why can I sort by an unselected column in one query but not in a UNION?
A UNION result is a new table built from several SELECTs. ORDER BY only sees the columns of that result, not the tables underneath.
Is ORDER BY 2 a good fix?
It works, but it depends on the column order. Naming the result column with AS is safer when the query might change.
Is it the same on Db2 for LUW?
Yes. SQL0208N has SQLSTATE 42707 and the same meaning.