Server: Msg 108, Level 15, State 1, Line n
The ORDER BY position number x is out of range of the number of items in the select list.
SQL Server allows to reference a column in the ORDER BY clause by its ordinal position in the final resultset. This message appears when you specify a position number in the ORDER BY clause that doesn’t have a corresponding column in the SELECT list. For illustration purposes see the examples below.
The SQL statement cannot be parsed and further execution is stopped.
Error of the severity level 15 are generated by the user and can be fixed by the SQL Server user.
All versions of SQL Server.
ORDER BY 0
(SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3) AS t (c1)
ORDER BY 2
The first statement is invalid because there is no column position 0 in the SELECT list. Likewise the second query is invalid, because there is no position number 2 in the SELECT list. For both queries an ORDER BY 1 would make the statements valid and executable.